Python Object
Python is an object oriented programming language. So, its main focus is on objects unlike procedure oriented programming languages which mainly focuses on functions.
In object oriented programming language, object is simply a collection of data (variables) and methods (functions) that act on those data.
Python Class
A class is a blueprint for the object. Let's understand it by an example:
Suppose a class is a prototype of a building. A building contains all the details about the floor, doors, windows, etc. we can make another buildings (as many as we want) based on these details. So building is a class and we can create many objects from a class.
An object is also called an instance of a class and the process of creating this object is known as instantiation.Python classes contain all the standard features of Object Oriented Programming. A python class is a mixture of class mechanism of C++ and Modula-3.
Define a class in Python
In Python, a class is defined by using a keyword class like a function definition begins with the keyword def.
Syntax of a class definition:
1. class ClassName:
2. <statement-1>
3. .
4. .
5. .
6. <statement-N>
A class creates a new local namespace to define its all attributes. These attributes may be data or functions.
See this example:
file Qptions Window Help Python 3.5.2
>>> class MyClass:
"This is ApkZube"
a=50
def func(self):
print('Hello ApkZube')
output:
>>> MyClass.a
50
>>>
There are also some special attributes that begins with double underscore (__). For example: __doc__ attribute. It is used to fetch the docstring of that class. When we define a class, a new class object is created with the same class name. This new class object provides a facility to access the different attributes as well as to instantiate new objects of that class.
See this example:
file Qptions Window Help Python 3.5.2
>>> class MyClass:
"This is ApkZube"
a=50
def func(self):
print('Hello ApkZube')
output:
>>> MyClass.a
50
>>>MyClass.__doc__
“This is ApkZube”
>>>MyClass.func
<function MyClass.func at 0x0000000010D4158>
Create an Object in Python
We can create new object instances of the classes. The procedure to create an object is similar to a function call.
Let's take an example to create a new instance object ob. We can access attributes of objects by using the object name prefix.
See this example:
>» ob = MyClass()
>>> MyClass.func
<function MyClass. func at Ox00000000010D41584>
>>> ob.func
<bound method HyClass. func of < __main__.MyClass object at Ox000000000347BE48>
>>> ob.func() Hello ApkZube >>>
Here, attributes may be data or method. Method of an object is corresponding functions of that class. For example: MyClass.func is a function object and ob.func is a method object.
Python Object Class Example
class Student:
def __init__(self, rollno, name):
self.rollno = rollno
self.name = name
def displayStudent(self):
print "rollno : ", self.rollno, ", name: ", self.name
emp1 = Student(121, "Ajeet")
emp2 = Student(122, "Sonoo")
emp1.displayStudent()
emp2.displayStudent()
Output:
rollno : 121 , name: Ajeet
rollno : 122 , name: Sonoo
Python is an object oriented programming language. So, its main focus is on objects unlike procedure oriented programming languages which mainly focuses on functions.
In object oriented programming language, object is simply a collection of data (variables) and methods (functions) that act on those data.
Python Class
A class is a blueprint for the object. Let's understand it by an example:
Suppose a class is a prototype of a building. A building contains all the details about the floor, doors, windows, etc. we can make another buildings (as many as we want) based on these details. So building is a class and we can create many objects from a class.
An object is also called an instance of a class and the process of creating this object is known as instantiation.Python classes contain all the standard features of Object Oriented Programming. A python class is a mixture of class mechanism of C++ and Modula-3.
Define a class in Python
In Python, a class is defined by using a keyword class like a function definition begins with the keyword def.
Syntax of a class definition:
1. class ClassName:
2. <statement-1>
3. .
4. .
5. .
6. <statement-N>
A class creates a new local namespace to define its all attributes. These attributes may be data or functions.
See this example:
file Qptions Window Help Python 3.5.2
>>> class MyClass:
"This is ApkZube"
a=50
def func(self):
print('Hello ApkZube')
output:
>>> MyClass.a
50
>>>
There are also some special attributes that begins with double underscore (__). For example: __doc__ attribute. It is used to fetch the docstring of that class. When we define a class, a new class object is created with the same class name. This new class object provides a facility to access the different attributes as well as to instantiate new objects of that class.
See this example:
file Qptions Window Help Python 3.5.2
>>> class MyClass:
"This is ApkZube"
a=50
def func(self):
print('Hello ApkZube')
output:
>>> MyClass.a
50
>>>MyClass.__doc__
“This is ApkZube”
>>>MyClass.func
<function MyClass.func at 0x0000000010D4158>
Create an Object in Python
We can create new object instances of the classes. The procedure to create an object is similar to a function call.
Let's take an example to create a new instance object ob. We can access attributes of objects by using the object name prefix.
See this example:
>» ob = MyClass()
>>> MyClass.func
<function MyClass. func at Ox00000000010D41584>
>>> ob.func
<bound method HyClass. func of < __main__.MyClass object at Ox000000000347BE48>
>>> ob.func() Hello ApkZube >>>
Here, attributes may be data or method. Method of an object is corresponding functions of that class. For example: MyClass.func is a function object and ob.func is a method object.
Python Object Class Example
class Student:
def __init__(self, rollno, name):
self.rollno = rollno
self.name = name
def displayStudent(self):
print "rollno : ", self.rollno, ", name: ", self.name
emp1 = Student(121, "Ajeet")
emp2 = Student(122, "Sonoo")
emp1.displayStudent()
emp2.displayStudent()
Output:
rollno : 121 , name: Ajeet
rollno : 122 , name: Sonoo
No comments:
Post a Comment