Defining class in python-: This is the advance python language features . A class in Python is effectively a data type. All the data types built into Python All classes and Python gives us a powerful tools to manipulate every aspect of a class’s behaviour. we can define a class with the class statement.
class netnic body
Body is a list of Python statements normally variable assignments and function definitions. No assignments or function definitions are required. The body can be just a single pass statement.
In the class identifiers are in Cap-Case this is the first letter of each component word is capitalised to make the identifiers stand out. After we define the class we can create a new object of the class type by calling the class name as a function
instance myclass()
Using a class instance as a structure or record-: In the Class instances can be used as structures or records. In C structures or Java classes the data fields of an instance do not need to be declared ahead of time. They can be created on the fly. As like example defines a class called Circle, creates a Circle instance, assigns a value to the radius field of the circle and then uses that field to calculate the circumference of the circle
>>> class Circle: ... pass ... >>> my_circle = Circle() >>> my_circle.radius = 5 >>> print(2 * 3.14 * my_circle.radius) 31.4
In Java and many other languages the fields of an instance/structure are accessed and assigned to by using dot notation.
We can initialize fields of an instance automatically by including an __init__ initialization method in the class body. This function is run every time an instance of the class is created with that new instance as its first argument self. The __init__ method is similar to a constructor in Java but it doesn’t really construct anything. It initializes fields of the class. Also unlike those in Java and C++, Python classes may only have one __init__ method. This example creates circles with a radius of 1 by default.
class Circle: def __init__(self): 1 self.radius = 1 my_circle = Circle() 2 print(2 * 3.14 * my_circle.radius) 3 6.28 my_circle.radius = 5 4 print(2 * 3.14 * my_circle.radius) 5 31.400000000000002
In first argument of __init__. self is set to the newly created circle instance when __init__ is run 1. Another code uses the class definition. We first create a Circle instance object 2. The next line makes use of the fact that the radius field is already initialized 3. we can also overwrite the radius field 4. as a result the last line prints a different result from the previous print statement 5.
Python has a constructor, the __new__ method, which is what is called on object creation and returns an uninitialized object. Unless we are subclassing an immutable type like str or int, or using a metaclass to modify the object creation process it’s rare to override the existing __new__ method.
Instance variable in python-: Instance feature are the most basic feature of object oriented programming.
class Circle: def __init__(self): self.radius = 1
In Python we can create instance variables as necessary by assigning to a field of a class instance:
instance.variable = value
If the variable doesn’t already exist it will be created automatically which is how __init__ creates the radius variable.