python tutorial - Python Class | Python Object Class - learn python - python programming




 python oop car class and object

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.
  • This means there is a construct in Python called a class that lets you structure your software in a particular way.
  • Using classes, you can add consistency to your programs so that they can be used in a cleaner way.

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 building (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, like function definitions begin with the keyword def, in Python, we define a class using the keyword class.
  • The first string is called docstring and has a brief description about the class. Although not mandatory, this is recommended.
  • Here is a simple class definition.
class MyNewClass:
'''This is a docstring. I have created a new class'''
pass
  • A class creates a new local namespace where all its attributes are defined. Attributes may be data or functions.
  • There are also special attributes in it that begins with double underscores (__). For example, __doc__ gives us the docstring of that class.
  • As soon as we define a class, a new class object is created with the same name. This class object allows us to access the different attributes as well as to instantiate new objects of that class.

Syntax of a class definition:

class ClassName:  
    <statement-1>  
	   .  
	   .  
	   .  
    <statement-N> 
  • A class creates a new local namespace to define its all attribute. These attributes may be data or functions.

See this example:

 python class object
  • 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:

 python classes and objects

Create an Object in Python:

  • We saw that the class object could be used to access different attributes.
  • It can also be used to create new object instances (instantiation) of that class. The procedure to create an object is similar to a function call.
>>> ob = MyClass()
  • This will create a new instance object named ob. We can access attributes of objects using the object name prefix.
  • Attributes may be data or method. Method of an object are corresponding functions of that class. Any function object that is a class attribute defines a method for objects of that class.
  • This means to say, since MyClass.func is a function object (attribute of class), ob.func will be a method object.

See this example:

 python classe
  • 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  


Related Searches to Python Object Class