GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||
GeezerVison is showing www.tutorialspoint.com — Python - Class Attributes
Selected Reading
Python - Class AttributesThe properties or variables defined inside a class are called as Attributes. An attribute provides information about the type of data a class contains. There are two types of attributes in Python namely instance attribute and class attribute. The instance attribute is defined within the constructor of a Python class and is unique to each instance of the class. And, a class attribute is declared and initialized outside the constructor of the class. Class Attributes (Variables)Class attributes are those variables that belong to a class and whose value is shared among all the instances of that class. A class attribute remains the same for every instance of the class. Class attributes are defined in the class but outside any method. They cannot be initialized inside __init__() constructor. They can be accessed by the name of the class in addition to the object. In other words, a class attribute is available to the class as well as its object. Accessing Class AttributesThe object name followed by dot notation (.) is used to access class attributes. ExampleThe below example demonstrates how to access the attributes of a Python class. class Employee:
name = "Bhavesh Aggarwal"
age = "30"
# instance of the class
emp = Employee()
# accessing class attributes
print("Name of the Employee:", emp.name)
print("Age of the Employee:", emp.age)
OutputName of the Employee: Bhavesh Aggarwal Age of the Employee: 30 Modifying Class AttributesTo modify the value of a class attribute, we simply need to assign a new value to it using the class name followed by dot notation and attribute name. ExampleIn the below example, we are initializing a class variable called empCount in Employee class. For each object declared, the __init__() method is automatically called. This method initializes the instance variables as well as increments the empCount by 1. class Employee:
# class attribute
empCount = 0
def __init__(self, name, age):
self.__name = name
self.__age = age
# modifying class attribute
Employee.empCount += 1
print ("Name:", self.__name, ", Age: ", self.__age)
# accessing class attribute
print ("Employee Count:", Employee.empCount)
e1 = Employee("Bhavana", 24)
print()
e2 = Employee("Rajesh", 26)
OutputWe have declared two objects. Every time, the empCount increments by 1. Name: Bhavana , Age: 24 Employee Count: 1 Name: Rajesh , Age: 26 Employee Count: 2 Significance of Class AttributesThe class attributes are important because of the following reasons −
Built-In Class AttributesEvery Python class keeps the following built-in attributes and they can be accessed using the dot operator like any other attribute −
Access Built-In Class AttributesTo access built-in class attributes in Python, we use the class name followed by a dot (.) and then attribute name. ExampleFor the Employee class, we are trying to access all the built-in class attributes − class Employee:
def __init__(self, name="Bhavana", age=24):
self.name = name
self.age = age
def displayEmployee(self):
print ("Name : ", self.name, ", age: ", self.age)
print ("Employee.__doc__:", Employee.__doc__)
print ("Employee.__name__:", Employee.__name__)
print ("Employee.__module__:", Employee.__module__)
print ("Employee.__bases__:", Employee.__bases__)
print ("Employee.__dict__:", Employee.__dict__ )
OutputIt will produce the following output − Employee.__doc__: None
Employee.__name__: Employee
Employee.__module__: __main__
Employee.__bases__: (<class 'object'>,)
Employee.__dict__: {'__module__': '__main__', '__init__': <function Employee.__init__ at 0x0000022F866B8B80>, 'displayEmployee': <function Employee.displayEmployee at 0x0000022F866B9760>, '__dict__': <attribute '__dict__' of 'Employee' objects>, '__weakref__': <attribute '__weakref__' of 'Employee' objects>, '__doc__': None}
Instance AttributesAs stated earlier, an instance attribute in Python is a variable that is specific to an individual object of a class. It is defined inside the __init__() method. The first parameter of this method is self and using this parameter the instance attributes are defined. ExampleIn the following code, we are illustrating the working of instance attributes. class Student:
def __init__(self, name, grade):
self.__name = name
self.__grade = grade
print ("Name:", self.__name, ", Grade:", self.__grade)
# Creating instances
student1 = Student("Ram", "B")
student2 = Student("Shyam", "C")
OutputOn running the above code, it will produce the following output − Name: Ram , Grade: B Name: Shyam , Grade: C Instance Attributes Vs Class AttributesThe below table shows the difference between instance attributes and class attributes −
Advertisements
| |||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |