Python Classes and Objects:
Python is an object oriented programming language.
Almost everything in Python is an object, with its properties and methods.
A Class is like an object constructor, or a "blueprint" for creating objects.
How to create a class:
To create a class, use the keyword
class
Example:
class FirstClass:
x = 5
x = 5
print(FirstClass)
Output:
<class '__main__.FirstClass'>
Create Object:
Example:
class FirstClass:
x = 10
x = 10
p = firstClass()
print(p.x)
print(p.x)
Output:
10
Note: Here p is an object of class (FirstClass).
Output:
Billy
36
Note:
Note: self represents the instance of the class. By using the “self” keyword we can access the attributes and methods of the class in python
Note: Here p is an object of class (FirstClass).
__init__() Function:
All classes have a function called __init__(), which is always executed when the class is being initiated.
Use the __init__() function to assign values to object properties, or other operations that are necessary to do when the object is being created:
Example:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
p = Person("Billy", 36)
print(p.name)
print(p.age)
def __init__(self, name, age):
self.name = name
self.age = age
p = Person("Billy", 36)
print(p.name)
print(p.age)
Output:
Billy
36
Note:
__init__()
function is called automatically every time the class is being used to create a new object.Note: self represents the instance of the class. By using the “self” keyword we can access the attributes and methods of the class in python
Object Methods
Objects can also contain methods. Methods in objects are functions that belong to the object.
Example:
class Person:
def __init__(self, name, age):
self.name = name
def myfunc(self):
print("Hello my name is " + self.name)
p = Person("Billy")
p.myfunc()
def __init__(self, name, age):
self.name = name
def myfunc(self):
print("Hello my name is " + self.name)
p = Person("Billy")
p.myfunc()
Output:
Hello my name is Billy
self parameter:
The
self
parameter is a reference to the current instance of the class, and is used to access variables that belongs to the class.
We can call self with any name, but it has to be the first parameter of any function in the class.
Example:
class Person:
def __init__(my, name, age):
my.name = name
my.age = age
def myfunc(abc):
print("Hello my name is " + abc.name)
p1 = Person("Billy")
p1.myfunc()
def __init__(my, name, age):
my.name = name
my.age = age
def myfunc(abc):
print("Hello my name is " + abc.name)
p1 = Person("Billy")
p1.myfunc()
Output:
Hello my name is Billy
Modifying object properties:
Example:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def myfunc(self):
print("Hello my name is " + self.name)
p1 = Person("Billy", 36)
p1.age = 40
print(p1.age)
def __init__(self, name, age):
self.name = name
self.age = age
def myfunc(self):
print("Hello my name is " + self.name)
p1 = Person("Billy", 36)
p1.age = 40
print(p1.age)
Output:
40
Delete Object Properties:
You can delete properties on objects by using the
del
keyword.
Example:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def myfunc(self):
print("Hello my name is " + self.name)
p1 = Person("Billy", 36)
del p1.age
print(p1.age)
Output:
Traceback (most recent call last):
File "demo_class7.py", line 13, in <module>
print(p1.age)
AttributeError: 'Person' object has no attribute 'age'
File "demo_class7.py", line 13, in <module>
print(p1.age)
AttributeError: 'Person' object has no attribute 'age'
0 Comments