background top icon
background center wave icon
background filled rhombus icon
background two lines icon
background stroke rhombus icon

Download "#1. Классы и объекты. Атрибуты классов и объектов | Объектно-ориентированное программирование Python"

input logo icon
Video tags
|

Video tags

класс
объект
ооп python
ооп
объектно оринтированое программирование
основы ооп
python ооп
python
уроки python
python программирование
python для начинающих
python обучение
python 3
python programming
python уроки программирования
python уроки для начинающих
питон
пайтон
питон уроки
уроки пайтон
экземпляры класса
экземпляр класса python
python переменные класса
что такое экземпляр класса в питоне
экземпляр в python
ооп питон
ооп питон задачи
Subtitles
|

Subtitles

subtitles menu arrow
  • ruRussian
Download
00:00:02
Sergey Balakirev and in this lesson we
00:00:04
will learn how to create objects in python and define
00:00:06
classes, that is,
00:00:09
instances of these classes, and also
00:00:11
add and delete their attributes, that is,
00:00:13
data. Suppose we want to define a
00:00:16
class for storing the coordinates of points on a
00:00:18
plane for I’ll start by writing it down without
00:00:21
any content, for this we
00:00:24
first write the following keyword class
00:00:26
and then, separated by a space, we define the name of
00:00:29
this class, but let it be called like this
00:00:32
point, but then here we have
00:00:34
the contents of the class, that is, we put: and
00:00:38
y we begin with a block for the class, that is,
00:00:41
everything that we define here inside us
00:00:43
will relate to this class point,
00:00:45
let it be empty for now, I
00:00:48
will put such an operator pass, we all have
00:00:51
defined a class named point and
00:00:54
look in accordance with the
00:00:57
IEC 8 standard name It is customary to start a class with a
00:01:00
capital letter and, of course, name it
00:01:03
so that it reflects the essence of this class.
00:01:06
In the future, I will adhere to this
00:01:08
rule and so we have the
00:01:10
simplest definition of a class, but in this
00:01:13
form it is not particularly useful, so I
00:01:16
will write two attributes in it 1 attribute
00:01:19
let him call the rock, that is, the color and
00:01:21
taking this value ed and the second
00:01:24
requires glasses that will determine
00:01:26
the radius. Well, let it be equal to two. Pay
00:01:30
attention to the variable inside the class, they are usually
00:01:33
called attributes of the class or its
00:01:37
properties. I will
00:01:39
use this terminology in the future.
00:01:40
Now in our class there are two of these
00:01:43
attributes of the glasses, but how to
00:01:46
correctly perceive this construction;
00:01:49
in fact, the class itself forms a
00:01:52
namespace in this case with
00:01:54
the name point and in this namespace
00:01:58
there are two variables cola and secl and
00:02:01
we can access them using
00:02:04
the syntax for the namespace, for example, this is how
00:02:07
I copy this class, go to the
00:02:09
python console and the correspondence is created,
00:02:12
this class is right here
00:02:13
directly in the python console, this is how it
00:02:15
will be It’s more convenient to show here on
00:02:17
the right here we have this class, here
00:02:20
it is point and, accordingly, it contains two of
00:02:22
these from the variable, that is, two
00:02:24
properties of glass and color and now
00:02:27
look, we can write the name of this
00:02:30
class a point, that is, the namespace
00:02:33
point, put a dot and here we
00:02:36
have two of our properties color and sekl, but
00:02:40
let me select color and assign it
00:02:42
some other value, for example,
00:02:44
black even enter and look here on the
00:02:47
right we have this color property
00:02:49
changed from red to black, or
00:02:52
we can read what -a property
00:02:55
from this class, that is, write down
00:02:57
its name again, put a dot, and define,
00:03:01
select some property here, let
00:03:03
the glass, press enter and we have it spinning
00:03:06
the value 2, which is exactly contained in
00:03:09
this property, and to see all
00:03:11
the attributes of the class, you can use
00:03:13
this a special variable
00:03:16
called dict, that is, two
00:03:18
underscores dict and 2 underscores
00:03:21
by pressing enter and we have such a
00:03:23
collection reminiscent of a dictionary and here
00:03:26
we have many, many different attributes and among
00:03:29
them there are two of ours, this is this color with the
00:03:32
value black and falcon with the value 2
00:03:35
Now let's take the next step and
00:03:38
create instances of this class. About our
00:03:42
case, to create an object, that is, an
00:03:44
instance of a class is enough
00:03:47
to put two
00:03:50
parentheses like this after its name, press enter and look on
00:03:53
the right we have this change and
00:03:55
which is just an instance of
00:03:59
this class point and through this
00:04:01
variable there are two such
00:04:03
attributes available, two properties color and circle
00:04:06
that are defined in this
00:04:08
point class, or we can create another
00:04:10
instance using exactly the same
00:04:13
syntax, but example b equals point and
00:04:16
two parentheses, press enter and
00:04:19
we have the second variable b which has a
00:04:22
completely different address, here we are in this
00:04:24
video, this means the address of variable b
00:04:26
and the address of variable a are different, this
00:04:30
means that object a and object b are
00:04:34
completely two times independent objects
00:04:37
and object b also contains these two
00:04:40
properties of glass and color by analogy,
00:04:43
it can create an arbitrary number of
00:04:45
instances of a class, since variables a
00:04:48
and b refer to objects of the class
00:04:51
point, we can clearly determine this
00:04:54
using such a function type, I write
00:04:56
type and ad, and then at the output we will see that this is nothing more than an
00:05:00
object is a class point,
00:05:02
that is, it belongs to a type of
00:05:04
this class point, and we can even
00:05:07
check it explicitly like this if we write type
00:05:09
from a-two equals and point at the output we get
00:05:13
a container or in this form from
00:05:16
instance a, point in in this case, we
00:05:20
will also get true as an output, that is, the name of the
00:05:23
classes here acts as a
00:05:25
data type, but let’s take a closer look at
00:05:28
what we have here and in
00:05:30
reality what happened is, firstly,
00:05:33
objects a and b form their own namespace, the
00:05:36
namespace of
00:05:39
class instances and, secondly, secondly, it does not contain any
00:05:43
of its own attributes, the color and
00:05:47
circle properties are taken directly from the
00:05:50
point class inside these objects a and b,
00:05:53
these properties do not exist, they do not exist, they
00:05:57
refer to the corresponding properties
00:05:59
attributes of this class point,
00:06:02
so it is not by chance that I call these two
00:06:05
variables
00:06:06
attributes of the class highlights the fact
00:06:09
that these changes are inside the
00:06:13
point class, that is, the attributes of the class are common
00:06:17
to all its instances and we can
00:06:21
easily verify this,
00:06:22
for example, let's change this
00:06:24
property of glass, it now
00:06:26
takes the value 2 and we will set the
00:06:29
value to 1, I press enter and look
00:06:32
here, then automatically in our
00:06:34
objects this value also changed from
00:06:36
two to one, and whether
00:06:40
you look at it or not, but this collection of dict from
00:06:42
this object, and for example, it will be
00:06:45
empty, this just means that the
00:06:47
object itself and now is not contains
00:06:51
no attributes and the same with
00:06:54
object b, it will also be empty, that is,
00:06:56
this dict collection will be empty, that is,
00:06:59
our objects a and b form spaces,
00:07:02
but in this namespace there is
00:07:05
nothing yet, but nevertheless we
00:07:08
can access attributes through them class
00:07:11
point that is, write a. for example,
00:07:15
color then we will see this value black,
00:07:17
that is, this value of the goods was taken
00:07:20
directly from the point class, or
00:07:22
we can write it like this.
00:07:24
respectively, we get the value 1, but
00:07:27
if we execute the following command a .
00:07:30
color and assign some other color,
00:07:32
for example green, then look what we
00:07:34
get, I press enter and on the right
00:07:38
here, object a of the color properties
00:07:41
takes Gorin and object b of the
00:07:44
color properties remains good, why did this happen, the
00:07:48
thing is that when we do
00:07:51
this command then through a
00:07:54
we access this namespace
00:07:57
to the namespace of the object a and then
00:08:00
this assignment operator
00:08:03
creates a variable named color in the
00:08:07
current namespace despite the fact
00:08:11
that we can take this
00:08:12
variable from another namespace
00:08:15
from an external space from the point class,
00:08:17
but this assignment operator
00:08:20
works like this: if the variable
00:08:23
color is not present in this current
00:08:26
namespace, then it is a variable, that
00:08:29
is, in this case, the attribute is created in
00:08:32
this current namespace and as a result,
00:08:36
object a already contains its own
00:08:39
attribute, then properties with this name
00:08:42
color, but object b still
00:08:45
refers to the color of this
00:08:48
point class, and we can also verify this
00:08:50
if we print the collection for this
00:08:54
object a, that is, we write a.a. dict and
00:08:58
look here we have these
00:09:00
local properties with the name color and the
00:09:03
value green, this is a point you need to
00:09:06
know and understand very well. On
00:09:09
this principle in python the
00:09:11
formation of class attributes and
00:09:14
local attributes of their instances is built. By
00:09:16
the way, by analogy we can create
00:09:19
new attributes and in the point class, to do
00:09:22
this, just write down the name of the class,
00:09:24
put a dot, but also come up with
00:09:27
some new property for us product
00:09:29
type pt and assign some
00:09:33
value to let the value be disk,
00:09:35
press enter and look, we have
00:09:38
these new objects here
00:09:40
properties type t with the value disk
00:09:43
and if we look at the point to class, in
00:09:46
this point class these
00:09:49
new properties have also appeared, that is, like this
00:09:51
we can dynamically add new
00:09:55
properties to this class, that is, a new
00:09:57
attribute or the same thing we can
00:09:59
do it using a special
00:10:01
function called set and tatar,
00:10:04
as the first argument we indicate
00:10:06
the namespace with which we want to
00:10:09
work, let it be the point class, and
00:10:11
then through the beaten second argument in the
00:10:14
form of a string, pay attention to the form of a
00:10:16
string, we must register that property
00:10:19
someone an attribute to which we want to assign
00:10:22
this or that value,
00:10:23
here I have written a completely new
00:10:25
probe attribute which is not yet in the
00:10:28
point class and let it take it, it
00:10:31
requires the value 1, that is, here are three such
00:10:34
arguments by pressing enter and look,
00:10:37
this attribute has appeared in the point class a
00:10:40
property of samples with a value of 1, that is,
00:10:43
if there is no attribute in the class to
00:10:47
which we want to assign a
00:10:49
value, then it is dynamically added to it,
00:10:52
and if we write here
00:10:55
some already existing
00:10:57
euro attribute like type t and assign new
00:10:59
values ​​to press quar then Let’s just
00:11:02
change the value of disk, which is here,
00:11:05
to the new values ​​of square, that is, this is how
00:11:08
we can work with class attributes,
00:11:11
dynamically adding them and changing
00:11:14
the value; if we want to read
00:11:16
any attribute value from a class,
00:11:19
then for this we just need to write the
00:11:21
class name or name again instance of a class, it doesn’t
00:11:24
matter, well, and accordingly indicate
00:11:27
the attribute that we want to access,
00:11:30
for example, glass at the output will get
00:11:32
this value 1, but or we can assign the
00:11:35
result of this attribute to some variable
00:11:37
and the corresponding variable
00:11:39
will contain the
00:11:40
value of this attribute, but when accessing a
00:11:45
non-existent class attribute, for example
00:11:47
but it doesn’t exist, we’ll get an error.
00:11:50
This error can be avoided if
00:11:54
we use a special function
00:11:56
called get to the top, it
00:11:59
allows you to read an attribute from the
00:12:01
specified namespace. In this
00:12:04
case, I specify a class, but you can also specify
00:12:06
objects of the class, it doesn’t matter, I specify
00:12:09
the class and given, separated by a comma, I will indicate a
00:12:12
non-existent attribute, and if we
00:12:15
write it like this, we will again get an error, but
00:12:19
here you can also enter a third argument, I will enter it like this:
00:12:22
fall and this third
00:12:25
argument will be returned if the
00:12:28
specified attribute is not detected, but what
00:12:31
if we indicate existing
00:12:34
properties here, for example color then it
00:12:36
corresponds to the return and you can
00:12:39
write off this goto top function in this form,
00:12:41
or you can
00:12:43
not write this third argument; in this case,
00:12:46
green will also be the result, but if this
00:12:48
color attribute is not in the point
00:12:51
to class with a plumb line, we will get an error
00:12:53
finally, we can not only add but
00:12:56
also remove any attributes from the class, and
00:12:58
this can be done in at least two
00:13:00
ways, the first is to use the article
00:13:03
about the dl operator, but also
00:13:05
indicate which attribute from which
00:13:07
namespace is being removed, in this
00:13:09
case we want to remove from the point class
00:13:12
here Let’s say this is the sample attribute by pressing
00:13:15
enter and look in our point class,
00:13:18
now this sample attribute is not there, we
00:13:21
have deleted it, but if we try to repeat
00:13:23
this command, we will already have an error
00:13:26
because we cannot delete a non-existent
00:13:29
kite sport attribute, the del command is impossible,
00:13:31
so before we emphasize this
00:13:34
or of another attribute, in theory, it would be
00:13:36
good to check whether it exists or
00:13:39
not, for this you can call
00:13:42
this special function called so
00:13:44
tomorrow, and accordingly, here
00:13:46
we indicate the namespace, that is, in
00:13:49
this case, I will indicate the class, and the
00:13:51
property that we are going to remove
00:13:53
samples for in the output it will give a fall because
00:13:59
there is no such property of such an attribute in the class, if it were there we will see
00:14:02
either the value true, but let me write it
00:14:04
here and press enter and we
00:14:07
have the value true because
00:14:09
such a property exists in the point class,
00:14:11
so here’s the first one the way to delete an attribute
00:14:14
is the dl operator and the second way is
00:14:17
to use a function called
00:14:19
del a tatar, here we indicate with the first
00:14:22
argument the namespace from which the
00:14:24
attribute is deleted, well, given this attribute
00:14:27
that we are going to delete, for
00:14:29
example, this is type pt, press
00:14:32
enter and look at
00:14:34
This attribute, etc., was indeed removed from our classes.
00:14:36
Of course, if we call the
00:14:40
same function again for the same
00:14:42
attribute, we will get an error, that is,
00:14:45
only those attributes that
00:14:47
are present in the class can be deleted, so before
00:14:50
deleting anything, we need to check whether
00:14:52
such an attribute exists in the class or not,
00:14:55
and the same thing with objects, whether
00:14:57
such an attribute exists in the object or does
00:15:00
not exist, and look, if we
00:15:02
now call this
00:15:04
swizer function for the point class and specify an attribute
00:15:08
that exists, let’s say color, then of
00:15:11
course we will get the value true, and if
00:15:13
we specify the name of the object here, for example, we
00:15:16
also see true yes, I forgot that we
00:15:19
created this color property
00:15:21
directly in this instance,
00:15:23
so of course we see a corpse here,
00:15:25
let’s write glass instead of color,
00:15:27
press enter, we see the same trope, that is,
00:15:30
this function and but it says
00:15:33
that we can, in principle, get
00:15:35
through the namespace and access from to
00:15:38
this attribute is called, but it does not say
00:15:41
that this attribute is located
00:15:44
directly from in this
00:15:47
namespace, but pay attention to this, that
00:15:50
is, if we are here, we see a
00:15:51
collection of dictates, look,
00:15:54
there is only one attribute in it and these are the
00:15:57
2 attributes of the falcon, there are no here,
00:16:00
however, the gas function from r returns
00:16:03
the value true that we, in principle,
00:16:05
through and we can access this
00:16:07
attribute of glass, that is, the functions is at
00:16:09
our work and this is exactly the way, but the
00:16:12
removal of attributes occurs
00:16:14
directly in the current space of the
00:16:16
honey fraer, if we write for and here
00:16:19
we indicate glass, that is, this is the
00:16:21
glass attribute, it is missing in the secant
00:16:24
namespace, then we will get an error,
00:16:26
but we can completely remove color
00:16:30
because it is present in this object,
00:16:32
execute this line pay attention
00:16:35
in our object and this color property
00:16:38
takes the value garena now we
00:16:41
will remove this property from the object and I
00:16:44
press enter and we get the following
00:16:47
in this object and this color property
00:16:50
remains but already takes the value black
00:16:54
I think you guessed why this
00:16:57
happened in then after removing the
00:16:59
color attribute from the object, and this color
00:17:02
became a brother directly from the
00:17:05
point class, that is, the search for one or another
00:17:08
attribute is performed as follows:
00:17:10
first, it is searched in the current space
00:17:13
honey, and if it is not here, then it is taken
00:17:17
from the next external one, which
00:17:20
corresponds to this one. here is the
00:17:22
internal space in this case, the
00:17:24
external space for any object is the class from
00:17:27
which it was formed, but in this
00:17:30
case from the point class, so this
00:17:32
color attribute for object a is taken
00:17:35
from the point class, this point
00:17:38
should also be well understood when working with
00:17:40
local properties objects and
00:17:42
class attributes, and so now that we
00:17:45
have figured out how attributes are created,
00:17:48
let’s return to our task of creating
00:17:50
point objects on the plane. I will again
00:17:53
define this point class again and
00:17:56
also re-create two objects object a
00:17:59
and object b. For these two objects we
00:18:03
automatically get these are two
00:18:05
common properties of glass and color, but
00:18:08
the coordinates of the points of these objects must
00:18:11
exclusively belong to this
00:18:14
instance, so we will write these coordinates
00:18:17
as follows, we will indicate the
00:18:19
namespaces, that is, of the object, and we
00:18:22
will put a point, I will write the first
00:18:24
coordinate as x and assign the value 1
00:18:27
accordingly you are here about so in object
00:18:30
and we have a new local
00:18:33
property with the name x and
00:18:36
value 1, and the same for the second
00:18:39
coordinate y, I’ll just write the value 2
00:18:42
as a result, object a contains two of its
00:18:46
own local properties x and y with
00:18:49
values ​​1 and 2 and that is, the coordinate of
00:18:52
the point, well, we can
00:18:54
write the same thing for object b, let there
00:18:57
be coordinates 10 and 20, that is, these are the
00:19:00
x y property that we defined in
00:19:02
this way, that is, we
00:19:04
dynamically created them for the
00:19:07
corresponding instances of the class, they
00:19:10
are independent between As a
00:19:13
result, each object
00:19:15
represents a point with independent
00:19:18
coordinates on the plane, but the colors and their
00:19:22
size are like common data for
00:19:25
all these objects, that is, in this
00:19:27
way we can create a local
00:19:30
individual property for each
00:19:32
object, and at the end of this lesson,
00:19:35
I’ll note that in any class of the python language
00:19:38
we can define its description in the form of
00:19:41
this starting line, if we
00:19:43
now define in the console just
00:19:46
such a class with this 1 line,
00:19:48
look, this class has
00:19:51
this change, it’s called dogs 2
00:19:53
underscores long 2 underline and
00:19:56
this variable doc just contains
00:19:59
that line, the description that we
00:20:01
write here at the beginning usually
00:20:03
does this when creating large programs
00:20:05
when they create such descriptions in key classes
00:20:08
so that in the
00:20:10
future it would be more convenient to return to
00:20:13
them, correct them and use them without
00:20:15
referring to special documentation and
00:20:17
so from this lesson you should have a good
00:20:19
idea of ​​how python classes are defined
00:20:21
and how class objects are created,
00:20:24
what a class attribute and an
00:20:27
object attribute represent themselves be able to access this
00:20:31
attribute using attacks from the function
00:20:34
adding read check for the presence
00:20:37
and removal of attributes and also know
00:20:40
what are these two changes responsible for?
00:20:42
If all this is clear to you, then feel free
00:20:44
to move on to the next lesson
00:20:46
[music]

Description:

Курс по Python ООП: https://stepik.org/course/116336/promo Узнаете, что такое класс и как создавать экземпляры классов. Функции type и isinstance для объектов класса. Создание атрибутов классов и объектов. Функции: getattr(), hasattr(), setattr(), delattr(). Свойства: __doc__ и __dict__. Инфо-сайт: https://proproprogs.ru/python_oop Telegram-канал: https://t.me/python_selfedu

Preparing download options

popular icon
Popular
hd icon
HD video
audio icon
Only sound
total icon
All
* — If the video is playing in a new tab, go to it, then right-click on the video and select "Save video as..."
** — Link intended for online playback in specialized players

Questions about downloading video

mobile menu iconHow can I download "#1. Классы и объекты. Атрибуты классов и объектов | Объектно-ориентированное программирование Python" video?mobile menu icon

  • http://unidownloader.com/ website is the best way to download a video or a separate audio track if you want to do without installing programs and extensions.

  • The UDL Helper extension is a convenient button that is seamlessly integrated into YouTube, Instagram and OK.ru sites for fast content download.

  • UDL Client program (for Windows) is the most powerful solution that supports more than 900 websites, social networks and video hosting sites, as well as any video quality that is available in the source.

  • UDL Lite is a really convenient way to access a website from your mobile device. With its help, you can easily download videos directly to your smartphone.

mobile menu iconWhich format of "#1. Классы и объекты. Атрибуты классов и объектов | Объектно-ориентированное программирование Python" video should I choose?mobile menu icon

  • The best quality formats are FullHD (1080p), 2K (1440p), 4K (2160p) and 8K (4320p). The higher the resolution of your screen, the higher the video quality should be. However, there are other factors to consider: download speed, amount of free space, and device performance during playback.

mobile menu iconWhy does my computer freeze when loading a "#1. Классы и объекты. Атрибуты классов и объектов | Объектно-ориентированное программирование Python" video?mobile menu icon

  • The browser/computer should not freeze completely! If this happens, please report it with a link to the video. Sometimes videos cannot be downloaded directly in a suitable format, so we have added the ability to convert the file to the desired format. In some cases, this process may actively use computer resources.

mobile menu iconHow can I download "#1. Классы и объекты. Атрибуты классов и объектов | Объектно-ориентированное программирование Python" video to my phone?mobile menu icon

  • You can download a video to your smartphone using the website or the PWA application UDL Lite. It is also possible to send a download link via QR code using the UDL Helper extension.

mobile menu iconHow can I download an audio track (music) to MP3 "#1. Классы и объекты. Атрибуты классов и объектов | Объектно-ориентированное программирование Python"?mobile menu icon

  • The most convenient way is to use the UDL Client program, which supports converting video to MP3 format. In some cases, MP3 can also be downloaded through the UDL Helper extension.

mobile menu iconHow can I save a frame from a video "#1. Классы и объекты. Атрибуты классов и объектов | Объектно-ориентированное программирование Python"?mobile menu icon

  • This feature is available in the UDL Helper extension. Make sure that "Show the video snapshot button" is checked in the settings. A camera icon should appear in the lower right corner of the player to the left of the "Settings" icon. When you click on it, the current frame from the video will be saved to your computer in JPEG format.

mobile menu iconWhat's the price of all this stuff?mobile menu icon

  • It costs nothing. Our services are absolutely free for all users. There are no PRO subscriptions, no restrictions on the number or maximum length of downloaded videos.