Understanding Of Class And Instance Variables In Python 3

Understanding Of Class And Instance Variables In Python 3

Understanding Of Class And Instance Variables In Python 3

In this video tutorial, we are going to cover classes and instance variables. But, before you are going to watch a video we would highly recommend to read through all of this article. And if you have not watched a perverse part then we recommend to watch it now as you will be lost in this part.

Introduction

We move on to one of the most interesting topics of the cycle – object-oriented programming (OOP) in Python 3. From the point of view of OOP, a class is a collection of data. The use of classes gives us first of all the advantages of an abstract approach to programming.

  1. Polymorphism: in different objects, the same operation can perform different functions. The word “polymorphism” is of Greek nature and means “having many forms”. A simple example of polymorphism is the function count (), which performs the same action for different types of objects: ‘abc’.count (‘ a ‘) and [1, 2,’ a ‘]. Count (‘ a ‘). The operator plus is polymorphic when adding numbers and adding lines.
  2. Encapsulation: you can hide the unnecessary internal details of the object from the outside world. This is the second basic principle of abstraction. It is based on the use of attributes within the class. Attributes can have different states in between calls to class methods, as a result of which the object of this class also receives different states – state.
  3. Inheritance: You can create custom classes based on the base classes. This allows us to avoid writing a re-code.
  4. Composition: an object can be composite and include other objects.

The object-oriented approach in programming implies the following algorithm of actions.

  • The problem is described using a conventional language using concepts, actions, and adjectives.
  • Classes are formulated on the basis of concepts.
  • On the basis of actions, methods are designed.
  • Methods and attributes are implemented.

We got a skeleton – an object model. On the basis of this model, inheritance is realized. To check the model:

  1. so-called use cases are written – a scenario of possible behavior of the model, where all functionality is checked;
  2. The functionality can be fixed or added.

The object-oriented approach is good where the project implies long-term development, consists of a large number of libraries and internal links.

The Python class mechanism is a mixture of the C ++ and Modula-3 classes.

The most important features of classes in python:

  • Multiple inheritances;
  • A derived class can override any base class methods;
  • Anywhere you can call a method with the same base class name;
  • All the attributes of the class in the python are public by default, i.e. are available from everywhere; all methods are virtual; overload the base.

Today we will consider the following aspects of object-oriented programming.

  1. What is a class?
  2. Attributes of the class.
  3. Self.
  4. Inheritance.
  5. Video Tutoring.

1. What is the class?

A class is a custom type. The simplest model for defining a class is as follows:

class class_name:
Instruction 1
.
Instruction No.

Each such record generates its own class object. The difference from C ++ is that in the pluses the class description is just an ad, and in a python, it’s an object creation. There is also another type of object – the instance of the class that is generated when you call:

instance_class = class_name ()

The class object and the class instance are two different objects. The first one is generated at the stage of the class declaration, the second one is called when the class name is called. The object of the class can be one, the instances of the class can be any number.

The instruction is, as a rule, the definition of a function. When defining a class, a new namespace is created and a class object is created that is the wrapper for all instructions.

Class objects support two types of operations:

  • access to attributes;
  • create an instance of the class.

2. Attributes of the class

Attributes of the class are of two types:

  • data attributes;
  • attribute-methods.

Data attributes are usually written from above. The memory for attributes is allocated at the time of their first assignment – either outside or inside the method. Methods start with the def.

Attributes are accessed by obj.attrname.

For Instance:

class Simple:
 u'Simple Class'
 var = 87
 def f(x):
 return 'Hello world'

Here Simple.var and Simple.f are user attributes. There are also standard attributes:


>>> print Simple.__doc__

Integer


>>> print Simple.var.__doc__
int(x[, base]) -> integer
...

Creating an instance of a class is similar to the way we do a function call:

smpl = Simple()

An empty small object will be created. If we want some actions to be performed during the creation, we need to define a constructor that will be called automatically:

class Simple:
def __init__(self):
self.list = []

When the sample object is created, an empty list will be created. You can pass arguments to the constructor:


class Simple:
def __init__(self, count, str):
self.list = []
self.count = count
self.str = str

>>> s = Simple(1,'22')
>>> s.count, s.str
1 22

The data attribute can be made private (i.e. inaccessible from the outside – for this you need to put two underscores on the left:

class Simple:
 u'A simple class with a private attribute'
 __private_attr = 10 
 def __init__(self, count, str):
 self.__private_attr = 20
 print self.__private_attr
  
s = Simple(1,'22')
print s.__private_attr

The last line will throw an exception – the __private_attr attribute is only valid for internal use.
Methods need not be defined inside the class body:


def method_for_simple(self, x, y):
return x + y

class Simple:
f = method_for_simple

>>> s = Simple()
>>> print s.f(1,2)
3

An empty class can be used as a blank for a data structure:

class Customer:
 pass
custom = Customer()
custom.name = 'Alex'
custom.salary = 100000

3. Self

Usually, the first argument to the method name is self. As the author of Guido Van Rossum says, this is nothing more than an agreement: the name of self-has absolutely no special meaning.

self is useful for accessing other class attributes:

 


class Simple:
def __init__(self):
self.list = []
def f1(self):
self.list.append(123)
def f2(self):
self.f1()

>>> s = Simple()
>>> s.f2()
>>> print s.list
[123]

4. Inheritance

The definition of a derived class is as follows:

class Derived(Base):

If the base class is not defined in the current module:

class Derived(module_name.Base):

The resolution of attribute names works from the top down: if the attribute is not found in the current class, the search continues in the base class, and so on by recursion. Derived classes can override the methods of base classes – all methods are virtual in this sense. You can call the base class method with the prefix:

Base.method()

In the python, there is limited support for multiple inheritances:

class Derived(Base1,Base2,Base3):

The attribute search is performed in the following order:

  • in Derived;
  • in Base1, then recursively in the base classes Base1;
  • in Base2, then recursively in the Base2 base classes
  • etc.

5. Video Tutoring