Classes in python
- Python is an object-oriented programming language.
- Almost everything in Python is an object, with its properties and methods.
- A class is a user-defined blueprint or prototype from which objects are created.
- Collection of data members and member function
- 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
Some points on Python class:
- Classes are created by keyword class.
- Attributes are the variables that belong to a class.
- Attributes are always public and can be accessed using the dot (.) operator. Eg.:
Myclass.Myattribute
Class Definition Syntax
Class ClassName:
#Statement-1
.
.
.
#Statement-N
Create a Class with Example
Create a Class
To create a class, use the keyword class:
Example
Create a class named MyClass, with a property named x:
class MyClass:
x = 5
Objects in python
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. It's not an idea anymore butwithout the class as a guide, you would be lost, not knowing what information is required.
An object consists of :
- State: It is represented by the attributes of an object. It also reflects the properties of an object.
- Behaviour: 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.
Create object with example
Create Object
Now we can use the class named MyClass to create objects:
Example
Create an object named p1, and print the value of x:
p1 = MyClass()
print(p1.x)
Member function and class variable:
Member function are methods that are accessed via a dot notation.
Class variable is a variable that is shared by an instance of a class.Class variable is defined within a class but outside the class's methods
class Dog:
# attribute/data member
attr1 = "mammal"
attr2 = "dog"
# A sample method /function
def fun(self):
print("I'm a", self.attr1)
print("I'm a", self.attr2)
Overview of OOP Terminology
- 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.
- Class variable − A variable that is shared by all instances of a class. Class variables are defined within a class but outside any of the class's methods. Class variables are not used as frequently as instance variables are.
- Data member − A class variable or instance variable that holds data associated with a class and its objects.
- Function overloading − The assignment of more than one behavior to a particular function. The operation performed varies by the types of objects or arguments involved.
- Instance variable − A variable that is defined inside a method and belongs only to the current instance of a class.
- Inheritance − The transfer of the characteristics of a class to other classes that are derived from it.
- Instance − An individual object of a certain class. An object obj that belongs to a class Circle, for example, is an instance of the class Circle.
- Instantiation − The creation of an instance of a class.
- Method − A special kind of function that is defined in a class definition.
- 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.
- Operator overloading − The assignment of more than one function to a particular operator.
Constructors in Python
- Class functions that begin with double underscore __ are called special functions as they have special meaning.
- Of one particular interest is the __init__() function. This special function gets called whenever a new object of that class is instantiated.
- This type of function is also called constructors in Object Oriented Programming (OOP). We normally use it to initialize all the variables.
Example:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
p1 = Person("John", 36)
print(p1.name)
print(p1.age)
The self Parameter
- The self parameter is a reference to the current instance of the class, and is used to access variables that belongs to the class.
- It does not have to be named self, you can call it whatever you like, but it has to be the first parameter of any function in the class:
Example:
class Person:
def __init__(mysillyobject, name, age):
mysillyobject.name = name
mysillyobject.age = age1
def myfunc(abc):
print("Hello my name is " + abc.name)
p1 = Person("John", 36)
p1.myfunc()
Modify Object Properties:
You can modify properties on objects like this:
Example
Set the age of p1 to 40:
p1.age = 40
Delete Object Properties:
You can delete properties on objects by using the del keyword:
Example
Delete the age property from the p1 object:
del p1.age
Delete Objects:
You can delete objects by using the del keyword:
Example
Delete the p1 object:
del p1
The pass Statement:
Class definitions cannot be empty, but if you for some reason have a class definition with no content, put in the pass statement to avoid getting an error.
Example
class Person:
pass