0% found this document useful (0 votes)
4 views10 pages

Class and Objects in Python

The document discusses the fundamentals of classes and objects in Python, emphasizing the role of class methods and the special __init__() constructor method for initializing class instances. It highlights the difference between class variables and object variables, noting that class variables are shared among instances while object variables are unique to each instance. Additionally, it covers the concept of private methods and attributes, advising against accessing them from outside the class.

Uploaded by

devanshkush123
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
4 views10 pages

Class and Objects in Python

The document discusses the fundamentals of classes and objects in Python, emphasizing the role of class methods and the special __init__() constructor method for initializing class instances. It highlights the difference between class variables and object variables, noting that class variables are shared among instances while object variables are unique to each instance. Additionally, it covers the concept of private methods and attributes, advising against accessing them from outside the class.

Uploaded by

devanshkush123
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
ses. The class mechanism st seascussed in Chapter 2, Pyth ‘one, we will study about Cases and objects are the ‘uilding block in Python. A Gases provides a blueprint o object or an instance of s ‘scully instances of class int. Sng methods using the vi ‘ued that we can find of agit, M80 Cathey ay fine hn! ie : CLASS METHOD AND SELF ce functions defined in the class) are exactly same ‘with just One Sonal di #9 ordinary functions that we have bees nent that is added to the beginni cc at wenn el ‘en you call he method ython provides hs dommes sete poe fe ould be defined to accept the self, Similarly, a function defined to accept one parameter nwo—self and the parameter, so on and so forth, ss methods uses self, they require an object or instance of the class to be used Por this program given below which has one cas warble and oe cs mae CO Se ‘cepts no values but still has sel as an angument: Bel SSS bject of the Claes Bi ar = 18 ‘ cet display(self) print(*In class Key points to remember + The statements inside the class definition must be properly indented. < + Acclass that has no other statements should have a pass statement at least, * Class methods of functions that begins with double underscore (__.) are special functions id a special meaning, 9.4 THE __init__() METHOD (THE CLASS CONSTRUCTOR) The __init__() method has a special significance in Python classes. The __init__() automatically executed when an object of a class is created, The method is useful to initialize the the class object. Note the _init__() is prefixed as well as suffixed by double underscores. The _4, method can be declared as, def __ init__(self, [args..+]). Look at the program Biven below tha the __init__() method. Program illustrating the use of _init__() method ‘class ABC(): def __init_(self,val): print("In class method.....") [Link] = val print("The value is : ", val) obj = ABC(10) OUTPUT San In class method..... The value is : In the program, the _init__() method accepts one argument val. Like any other class method the first argument has to be self, In the init__() method we define a variable as self. val which has exactly ie same name as that specified in the argument list. Though the two. the _init_() method. This is because the _init__() method is automatically involved of the class is created. It is a good programming habit to initialize all attributes in the values can be initialized in other methods also but it is not .5 CLASS VARIABLES AND OBJECT VARIABLES We have seen that a class can have variables defined in it. Basically, these variables are of variables and object variables. As tffe name suggests, class variables are owned by are owned by eachrobject. What this specifically means can be understood by using | iicxim thn, tpt on elise eg ae object variabl ‘ * The object variable is not shared between objects. * A change made to the object variable by one object will not be reflected in other objects. : H Python Programming ined in the el Pri jes that are defi ate variables, on the other hand, are those variables [mt clas —.). These variables can be accesséit only from wi san, . i: = uublic and private variables Program to illustrate the difference between Pr ABic(): init__(self, vari, var2): ‘[Link] = vari self.__var2 = var2 def display(self): print("From class method, vari = ® print("From class method, var2 = obj = ABC(10, 20) [Link]() Print(“From main module, Vari = ", [Link]) print(“From main module, Var2 = ", obj.—var?) ", self-vart) ¥ ", self._var2) OUTPUT From class method, Var1 = 10 From class method, Var2 = 20 From main module, Vari = 10 From main module, Var2 = Traceback (most recent call last): File "C:\Python34\[Link]", line 11, in print("From main module, var2 = ", obj._var2) AttributeError: ABC instance has no attribute '_var2" ‘As a good programming habit, you should never try to access a private variable from an class. But if for some reason, you need to do it, then you can access the private variable syntax, objectnane._¢lassname__privatevariable So, to remove the error from the above code, you could have written the last statement as print(”From main module, Var2 = ", obj._ABC__var2) 9.9 PRIVATE METHODS Remember that, private attributes should not be accessed from anywhere outside attributes, you can even have private methods in your class, “Usually we keep those method which haye implementation details, So like private attributes, you should also not use a prival anywhere outside the class. However, if itis very necessary to access them from : are accessed with a small difference, A private method can be accessed usit the: class name from outside the class. The syntax for accessing the private ale iin case saan i ib menamraeae:

You might also like