Classes and Objects
Hello guys,
here we will discuss about classes and objects in python. Python is an object-oriented
programming language. Almost every thing in python is an object.
A class is
like an object constructor, or an overview for creating objects.
A class creates a new local namespace where all its
attributes are defined. Attributes may be data or functions.
Syntax for
creating class is, class:
Example,
Output
Creating an object
The
procedure to make an object is similar to function call.
Person =
My_class ()
Output of the program is simple, i.e., 8 and 9
We can access the attributes of objects using the object name prefix.
Attributes maybe data or method.
Check the output and understand and practice.
Y’all would
have, for sure, didn’t notice the unbound method, right? Check out, Bound,
unbound and static methods in python.
Constructors
in Python
Class
functions that begin with double underscore __ are called special functions as they have special meaning.
To
understand the meaning of classes we have to understand the built-in __init__ () function.
This type of
function is also called constructors in Object Oriented Programming (OOP). We
normally use it to initialize all the variables.
Understand the output
Note: - We can always add a new attribute if
we forgot to add it earlier
We can also
modify the given attributes by just changing the value.
To delete
attributes and objects we use the simple del,
Output
Similarly,
Output
On the command del f2, this binding is removed and the name f2 is deleted from the corresponding namespace. The object however continues to exist in memory and if no other name is bound to it, it is later automatically destroyed.
Note: - class definitions cannot be empty,
but if you for some reason have a class definition with no content, put in the
pass statement to avoid getting an error.
Hello Python people, for this blog we have taken help from the book Python : The Complete Reference.
If you still have any doubt on this topic then do come to us via email "sophomoretechs@gmail.com" or via Instagram "@coding.winds".
Do subscribe to our daily blog update by clicking here.
Thank You!
No comments:
Post a Comment