Classes in Python

walden systems, geeks corner, programming, languages, Python, scope, local scope, global scope, hoisting, functions, variables, developer, scripting, decrypt, aes256, encrypt, pycrypto, pip, tuple, dictionary, list, file, read, write, readline, while, for, loops, break, continue, udf, functions, argument, return value, class, method, instance, object
Python is a programming language that lets you work quickly and integrate systems more effectively.

A class is a blueprint for objects, one class for any number of objects of that type. You can also call it an abstract data type. Interestingly, it contains no values itself, but it is like a prototype for objects. A class is a user defined blue 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. Before we begin, there are some terms that we need to get familiar with when it comes to classes and objects in Python or any other object oriented programming or OOP.

Object : A unique instance of a class. An object has both data members and methods.

Instance : An individual object of a particular class.

Instantiation : The creation of an object of a particular class.

Class variable : Before we begin, there are some terms that we need to get familiar with when it comes to classes and objects in Python or any other object oriented programming or OOP.

Instance variable : A variable that is defined inside a method and belongs only to the current instance of a class. Data member : A class variable or instance variable that holds data associated with a class and its objects.

Method : A function that is defined in a class definition.

Inheritance : Getting of the characteristics of a class to other classes that are derived from it.

Creating classes

To define a class in Python, we use the class keyword. This is like using def to define a function. And like a function, a Python class may have a docstring as well. As soon as we define a class, a Python class object is created. Keep in mind that we can only name a class according to the identifier naming rules.

class MyClass :
   myVar = 0
    
   def __init__( self, param1 ) :
      self.myInstVar1 = param1


Constructor

A constructor is a special method that Python calls when we instantiate an object using the definitions found in our class. Python relies on the constructor to perform tasks such as initializing ( assigning values to ) any instance variables that the object will need when it starts. Constructors can also verify that there are enough resources for the object and perform any other start up task we can think of. The name of a constructor is always the same, __init__(). The constructor can accept arguments to create the object if we want. When we create a class without a constructor, Python automatically creates a default constructor for us that doesn't do anything. Every class must have a constructor, even if it simply relies on the default constructor.

1    class MyClass :
2        myVar = 0
3    
4        def __init__( self, param1 ) :
5            self.myInstVar1 = param1


Line 4 is the constructor definition.

Self keyword

As we can see in the examples above, there is a parameter called self in the constructor. Python requires that self be passed as a parameter for every method defined in the class. It is a self referencing pointer. It is necessary to pass self as a parameter if the method is inside a class because when an object calls a method, Python automatically passes an instance of it. So, if we do not write self, we will get an error. The reason for self is that we may have several instances of a class, self is a pointer to the current object of the class so Python can keep track of which object method we are calling.

Objects

To create an instance of a class, we call the class using class name and pass in whatever arguments its __init__ method accepts. We can access the object's attributes using the dot operator with object.

#!/usr/bin/python

class Pet:
   petCount = 0

   def __init__(self, species, name):
      self.species = species
      self.name = name
      Pet.petCount += 1

   def displayPet(self):
      print "Name : ", self.name,  ", Type : ", self.species

pet1 = Pet("dog", "Spot")
pet2 = Pet("cat", "Fluffy")
pet1.displayPet()
pet2.displayPet()
print "Total pets %d" % Pet.petCount


The above code will output :
Name : Spot, Type : dog
Name : Fluffy, Type : cat
Total pets 2