Python Core



Admission Enquiry Form


Classes in python

  1. Python is an object-oriented programming language.
  2. Almost everything in Python is an object, with its properties and methods.
  3. A class is a user-defined blueprint or prototype from which objects are created.
  4. Collection of data members and member function
  5. 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: 

  1. Classes are created by keyword class.
  2. Attributes are the variables that belong to a class.
  3. Attributes are always public and can be accessed using the dot (.) operator. Eg.:

    Myclass.Myattribute

Class Definition Syntax

 

Class ClassName:
#Statement-1
.
.
.
#Statement-N

class in python example

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 :

  1. State: It is represented by the attributes of an object. It also reflects the properties of an object.
  2. Behaviour: It is represented by the methods of an object. It also reflects the response of an object to other objects.
  3. Identity: It gives a unique name to an object and enables one object to interact with other objects.

objects in python example

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)



class and object example

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

  1. 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.
  2. 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.
  3. Data member − A class variable or instance variable that holds data associated with a class and its objects.
  4. 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.
  5. Instance variable − A variable that is defined inside a method and belongs only to the current instance of a class.
  6. Inheritance − The transfer of the characteristics of a class to other classes that are derived from it.
  7. 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.
  8. Instantiation − The creation of an instance of a class.
  9. Method − A special kind of function that is defined in a class definition.
  10. 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.
  11. Operator overloading − The assignment of more than one function to a particular operator.

Constructors in Python

  1. Class functions that begin with double underscore __ are called special functions as they have special meaning.
  2. Of one particular interest is the __init__() function. This special function gets called whenever a new object of that class is instantiated.
  3. 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

  1. The self parameter is a reference to the current instance of the class, and is used to access variables that belongs to the class.
  2. 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