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:

[sourcecode language=”python”]
class class_name:
Instruction 1
.
Instruction No.
[/sourcecode]

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:

[sourcecode language=”python”]
instance_class = class_name ()
[/sourcecode]

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:

[sourcecode language=”python”]
class Simple:
u’Simple Class’
var = 87
def f(x):
return ‘Hello world’
[/sourcecode]

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

[sourcecode language=”python”]

>>> print Simple.__doc__

[/sourcecode]

Integer

[sourcecode language=”python”]

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

[/sourcecode]

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

[sourcecode language=”python”]
smpl = Simple()
[/sourcecode]

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:

[sourcecode language=”python”]
class Simple:
def __init__(self):
self.list = []
[/sourcecode]

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

[sourcecode language=”python”]

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

[/sourcecode]

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

[sourcecode language=”python”]
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
[/sourcecode]

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:

[sourcecode language=”python”]

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

class Simple:
f = method_for_simple

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

[/sourcecode]

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

[sourcecode language=”python”]
class Customer:
pass
custom = Customer()
custom.name = ‘Alex’
custom.salary = 100000
[/sourcecode]

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:

 

[sourcecode language=”python”]

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]

[/sourcecode]

4. Inheritance

The definition of a derived class is as follows:

[sourcecode language=”python”]
class Derived(Base):
[/sourcecode]

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

[sourcecode language=”python”]
class Derived(module_name.Base):
[/sourcecode]

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:

[sourcecode language=”python”]
Base.method()
[/sourcecode]

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

[sourcecode language=”python”]
class Derived(Base1,Base2,Base3):
[/sourcecode]

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

 

[embedyt] https://www.youtube.com/watch?v=q4gmbNqDAxE[/embedyt]