- Class − A user-defined prototype for an object that defines a set of attributes that characterize any object of the class. The attributes are data members (class variables and instance variables) and methods, accessed via dot notation.
- Object − A unique instance of a data structure that’s defined by its class. An object comprises both data members (class variables and instance variables) and methods.
A First Look at Classes :
Classes introduce a little bit of new syntax, three new object types, and some new semantics.
The simplest form of class definition looks like this:
In practice, the statements inside a class definition will usually be function definitions, but other statements are allowed, and sometimes useful — we’ll come back to this later. The function definitions inside a class normally have a peculiar form of argument list, dictated by the calling conventions for methods — again, this is explained later.
- Process Behind Creation of Class in Python −
When a class definition is entered, a new namespace is created, and used as the local scope — thus, all assignments to local variables go into this new namespace. In particular, function definitions bind the name of the new function here.
When a class definition is left normally (via the end), a class object is created. This is basically a wrapper around the contents of the namespace created by the class definition; we’ll learn more about class objects in the next section. The original local scope (the one in effect just before the class definition was entered) is reinstated, and the class object is bound here to the class name given in the class definition header (
ClassName in the example).
# Python3 program to demonstrate defining a class
Class Objects :
An Object is an instance of a Class. A class is like a blueprint while an instance is a copy of the class with actual values .
An object consists of :
- State: It is represented by the attributes of an object. It also reflects the properties of an object.
- Behavior: It is represented by the methods of an object. It also reflects the response of an object to other objects.
- Identity: It gives a unique name to an object and enables one object to interact with other objects.
Declaring Objects (Also called instantiating a class)
When an object of a class is created, the class is said to be instantiated. All the instances share the attributes and the behavior of the class. But the values of those attributes, i.e. the state are unique for each object. A single class may have any number of instances.
Detail Example :
"This is a person class"
age = 10 def greet(self):
Class objects support two kinds of operations:
attribute references and instantiation.
- Attribute references use the standard syntax used for all attribute references in Python:
obj.name. Valid attribute names are all the names that were in the class’s namespace when the class object was created.
“””A simple example class”””
i = 12345
return ‘hello world’
MyClass.f are valid attribute references, returning an integer and a function object, respectively. Class attributes can also be assigned to, so you can change the value of
MyClass.i by assignment.
__doc__ is also a valid attribute, returning the docstring belonging to the class:
"A simple example class".
2.Class instantiation uses function notation. Just pretend that the class object is a parameter less function that returns a new instance of the class. For example (assuming the above class):
x = MyClass()
creates a new instance of the class and assigns this object to the local variable
The instantiation operation (“calling” a class object) creates an empty object. Many classes like to create objects with instances customized to a specific initial state. Therefore a class may define a special method named
__init__(), like this:
self.data = 
When a class defines an
__init__() method, class instantiation automatically invokes
__init__() for the newly-created class instance. So in this example, a new, initialized instance can be obtained by:
x = MyClass()
>>> class Complex:
... def __init__(self, realpart, imagpart):
... self.r = realpart
... self.i = imagpart
>>> x = Complex(3.0, -4.5)
>>> x.r, x.i
Hence, we have covered the basic concepts of python classes & object-oriented programming, object initialization in python, assigning one object to another object in python, assigning attributes to an object on the fly, deleting a python object.