Python class
Class is a blueprint or Skelton.
Which gives information about how will be the class object.
Some of the class examples are Fruit, Human, Cars.
Object structure will be the same as class structure.
Python Object
The real-world entity is called object, object has property and behaviour.
Real-world entity means things which exist in real forms.
some examples are Apple, Men, BMW car.
all these entities exist in real-world with some kind of properties and behaviour.
property and behaviour of an object define or gives information about object that which kind of object is this.
Declaration of class in python
Syntax
class Class_name:
member variable
member methods
Example
class Person:
name=""
def set_name(self):
self.name="Aman"
Creating a new object of the class in python
Syntax
object_name=Class_name()
Example
obj=Person()
Accessing a member of a class
Accessing member (member variable, member methods) of a class means to access(set or get ) value of member variable and methods.
Member variable can be accessed inside or outside of class.
Accessing member variable and methods inside of a class
class Person:
name=""
def set_name(self):
self.name="Aman" # accessing member variable
def print_name(self):
self.set_name() # accessing member method
print(self.name) # accessing member variable
p=Person()
p.print_name()
Accessing member variable and method outside of a class
class Person:
name=""
def print_name(self):
print(self.name)
p=Person()
p.name="John" # accessing member variable of a class
p.print_name() # accessing member methods of a class
Self keyword in a python class
self keyword is a current class object, it is same as java this keyword which point to current class as an object.
when you are accessing member variables and methods inside a class, it is mandatory to use of a self keyword.
class Person:
name=""
def set_name(self): # current class object pointer
self.name="Aman" # accessing member variable of a class using self keyword
def print_name(self):
self.set_name() # accessing member methods of a class using self keyword
print(self.name)
p=Person()
p.print_name()
Parametrized function in a python class
class Calculator:
x=0
y=0
z=0
def add(self, first_number, second_number):
self.x=first_number
self.y=second_number
self.z=self.x+self.y
print("sum:",self.z)
def sub(self, first_number, second_number):
self.x=first_number
self.y=second_number
self.z=self.x-self.y
print("Subtract:",self.z)
c=Calculator()
c.add(10,20)
c.sub(20,10)
the return statement in class methods in a python
As we already learned at the python function tutorial about the return statement, the return statements also can be used within class methods.
class Calculator:
x=0
y=0
z=0
def add(self, first_number, second_number):
self.x=first_number
self.y=second_number
self.z=self.x+self.y
return self.z
def sub(self, first_number, second_number):
self.x=first_number
self.y=second_number
self.z=self.x-self.y
return self.z
c=Calculator()
x=c.add(10,20)
print("sum:",x)
y=c.sub(20,10)
print("subtract:",y)