Attributes in python
In this article, we shall learn about attributes in python. Classes are a mix of data and functions, which in reality mean attributes and methods respectively.
Typically, the body of a class will be a series of function definitions. When functions are defined inside of a class, then they are called methods. Think of the class as owning these methods.
Example:
# let C be a class
class C:
def fun1():
print( " This is function 1 " )
def fun2():
print( " This is function 2 " )
def fun3():
print( " This is function 3 " )
def fun3():
print( " This is function 3 " )
# fun1, fun2, fun3 and fun4 are methods of class C
Now, objects are created from classes. These objects have attributes, which are just variables stored inside an object. The syntax for accessing and using attributes is the name of the object variable, followed by a dot, followed by the name of the attribute.
SYNTAX of accessing an attribute:
object.attribute
Example:
# let C be a class
obj = C()
obj.name = “ Rishika “
obj.age = 19
obj.age = obj.age + 1
print( f" The name given to obj is { obj.name } " )
print( f" The updated age of obj is { obj.age } " )
OUTPUT:
The name given to obj is Rishika
The updated age of obj is 20
So, attributes can be of the following types:
Class attributes:
In an object-oriented programming language, when an attribute belongs to a class rather than any object, then it is known as a class attribute.
For example, you created a class called cars. You want to keep track of the number of cars created using your class, then you define a class attribute to the class to keep track of the number of objects created out of it.
Unlike other object-oriented programming languages, we can access class attributes through instance attributes.
Example:
class Car:
origin = " Vehicle "
def fun1():
print( " This is function 1 " )
def fun2():
print( " This is function 2 " )
def fun3():
print( " This is function 3 " )
# origin is the class attribute of the Car
c = Car()
print( c.origin )
OUTPUT:
Vehicle
Process finished with exit code 0
Instance attributes:
Instance attributes are set when an instance of an object is created. These are defined in the constructors of the given class.
Example:
class Car:
origin = " Vehicle "
# defining a constructor
def __init__(self, color, seats ):
self.color = color
self.seats = seats
def fun1():
print( " This is function 1 " )
def fun2():
print( " This is function 2 " )
def fun3():
print( " This is function 3 " )
c1 = Car( " Red ", " 4 " )
c2 = Car( " Blue ", " 7 " )
c1_color = c1.color
c2_color = c2.color
c1_seats = c1.seats
c2_seats = c2.seats
print( f" c1 is { c1_color } in color and has { c1_seats } seats " )
print( f" c2 is { c2_color } in color and has { c2_seats } seats " )
Output:
c1 is Red in color and has 4 seats
c2 is Blue in color and has 7 seats
Process finished with exit code 0
Private attribute:
Private attributes are declared under the concept of encapsulation. A private attribute, as the name suggests is private to the class, i.e., you can use this attribute inside the class, but if you use it outside the class, it gives you an error. So, you cannot access a private attribute outside a class.
We declare a private attribute by providing two underscore symbols before its name.
SYNTAX:
self.__privateattribute = privateattribute
Example:
class Example:
def __init__( self, x):
self.a = 10
self.b = 20
self.__c = 30
def fun1( self ):
print( f" The private attribute of Example class is { self.__c } " )
e = Example( 25 )
print( e.a )
print( e.b )
print( e.__c)
OUTPUT:
Traceback (most recent call last):
File "xxx\main.py", line 11, in <module>
print( e.__c)
AttributeError: 'Example' object has no attribute '__c'
10
20
Process finished with exit code 1
class Example:
def __init__( self, x):
self.a = 10
self.b = 20
self.__c = 30
def fun1( self ):
print( f" The private attribute of Example class is { self.__c } " )
e = Example( 25 )
print( e.a )
print( e.b )
e.fun1()
OUTPUT:
10
20
The private attribute of the Example class is 30
Process finished with exit code 0