Python Object Oriented Programming: Classes, Description, and Features

In Python, classes are a fundamental concept. This is the basis of the standard library, the work of most popular programs and the language itself. If you want to become more than just a novice programmer, you must understand the essence and principle of working with classes and objects.

python classes




What are classes

This is the basic software component of OOP. In Python, classes are used to implement new types of objects and are created using a special class statement. Outwardly, they resemble standard built-in data types, such as numbers or sequences. But class objects have a significant difference - support for inheritance.

- Python . . - . Python .

Python – . def. , , . ? – . .

python class methods








, , . , : object.attribute. .

, . . object.attribute, attribute class. attribute, , , .

:

  • , ;
  • – ;
  • – .

python objects classes




The image shows the Python class tree. As you can see from the example, Class 2 and 3 are superclasses. At the bottom are two instances of Instance 1 and 2, in the middle there is a subclass of Class 1. If you write the expression Instance2.w, it will force the interpreter to look for the value of the .w attribute in the following order:

  1. Instance2;
  2. Class1;
  3. Class2;
  4. Class3.

The name .w will be found in the superclass of Class3. In OOP terminology, this means that Instance 2 “inherits” the .w attribute from Class3.





Note that the instances in the drawing inherit only four attributes: .w, .x, .y and .z:

  • For instances of Instance1.x and Instance2.x, the .x attribute will be found in Class 1, where the search will stop because Class 1 is in the tree lower than Class 2.
  • For Instance1.y and Instance2.y, the .y attribute will be found in Class 1, where the search will stop, because this is the only place it appears.
  • Instance1.z Instance2.z .z Class 2, , Class3.
  • Instance2.name .name Instance2 .

. , Class 1 .x, .x Class 2.

,

: . , Python . , int. int.

, . . Python . , . , , .

. – , , – . . Python class.

python class attributes




self

– self. . , . Self – , . .

self , , ClassA methodA:

  • >>>class ClassA;
  • def methodA (self, 1, 2).

objectA ClassA :

  • >>>objectA.methodA(1, 2).

, : ClassA.methodA(objectA, 1, 2). self .

python class inheritance




,

Python. «» class:

  • >>>class :
  • def setinf(self, ): #
  • self.data =
  • def display(self): #
  • print(self.data) # .

def, setinf display. .setinf .display. , , .

, , :

  • >>>x = () # ;
  • >>>y = () # .

. :

  • >>>x.setinf(« ») # , self – x.
  • >>>y.setinf(3.14) #: .setinf(y, 3.14)

x, y .setinf , .

  • >>>x.display() # x y self.data
  • >>>y.display()
  • 3.14.

python classes examples




Python . . , .

__init__ __sub__. . Python __init__ . __sub__ .

  • >>>class : #
  • def __init__(self, start):
  • self.data = start
  • def __sub__(self, other): # other
  • return (self.data - other) #
  • >>>A = (10) #__init__(A, 10)
  • >>>B = A – 2 #__sub__(B, 2)
  • >>>B.data #B –
  • 8.

__init__

__init__ . . __init__ . , .

. , , .

. . .

python class constructor




__getitem__

__getitem__ . , . , F , F[i], Python __getitem__, F , , .

«» :

  • >>>class :
  • def __getitem__(self, index):
  • return index ** 2
  • >>>F = ()
  • >>>F[2] # F[i] F.__getitem__(i)
  • 4
  • >>>for i in range(5):
  • print(F[i], end=« ») # __getitem__(F, i)
  • 0 1 4 9 16

, . :

  • >>> = [13, 6, «», «», 74,9]
  • >>>[2:4]
  • [«», «»]
  • >>>[1:]
  • [6, «», «», 74,9]
  • >>>[:-1]
  • [13, 6, «», «»]
  • >>>[::2]
  • [13, «», 74,9]

, __getitem__:

  • >>>class :
  • _ = [13, 6, «», «», 74,9]
  • def __getitem__(self, ): #
  • print(«getitem: », )
  • return self._[] #
  • >>>X = ()
  • >>>X[0] # __getitem__
  • getitem: 0
  • 13
  • >>>X[2:4] # __getitem__
  • getitem: slice(2, 4, None)
  • [«», «»]

python class creation




__getattr__. . , __getattr__. .

:

  • >>>class Gone:
  • def __getattr__(self, atname):
  • if atname == «age»:
  • return 20
  • else:
  • raise AttributeError, atname
  • >>>D = Gone()
  • >>>D.age
  • 20
  • >>>D.name
  • AttributeError: name

Gone D . D.age __getattr__. self, «age» atname. D.age, , .

, __getattr__ , , . D.name .

__setattr__, . , «self. = » self.__setattr_(«», ).

. .

. Python, .

If you're not comfortable with the fact that indexing in sequences starts from scratch, you can fix this with the class statement. To do this, create a subclass of type list with new names of all types and implement the necessary changes. Also in OOP in Python, there are function decorators, static methods, and many other complex and special techniques.




All Articles