, . - . ; , . . , , .
. , . , int ; , , int () .
(
int) . , ++ ( 1.5- ) .
help(int) .# 1.5 , .
, . . . , . , , . , .
- / . .
class . .
self
- , , . , , , self .
, self - . - , self , () .
++//#
self , ++ this , # this .
self , . . MyClass , myobject . myobject.method(arg1, arg2) , MyClass.method(myobject, arg1, arg2)- - self - .
, , - self.
(_.py ).
class :
pass #
= ()
print()
:
$ python _.py
<__main__. instance at x1171f518>
class class. , . , pass .
, , / . ( ). , . __main__ .
. , .
/ , self . (_.py ).
class :
def _(self):
print(', ?')
p = ()
p._()
# 2
# ()._()
:
$ python _.py
, ?
self . _ , self .
__init__
. __init__ .
__init__ . ( ) . .
(save as _.py):
class :
def __init__(self, ):
self. =
def _(self):
print(', ', self.)
= ('')
._()
# 2
# ('')._()
:
$ python _.py
,
, __init__ (self ) name . , name . name , . , self.name , self name , name . , .
p , , : p = ('').
__init__ .
.
, self.name , _ .
( ) , . , , . , . .
- . , .
- . , , .
, / . , , . (_.py ):
class :
""" ."""
#
=
def __init__(self, name):
""" ."""
self.name = name
print("({} )".format(self.name))
# ,
#
. += 1
def (self):
""" ."""
print("{} !".format(self.name))
. -= 1
if . == :
print("{} .".format(self.name))
else:
print(" {:d} .".format(
.))
def _(self):
""" .
, ."""
print(", {} .".format(self.name))
@classmethod
def (cls):
""" ."""
print(" {:d} .".format(cls.))
1 = ("2-2")
1._()
.()
2 = ("-3")
2._()
.()
print("\n .\n")
print(" . .")
1.()
2.()
.()
:
$ python _.py
(2-2 )
, 2-2 .
1 .
(-3 )
, -3 .
2 .
.
. .
2-2 !
1 .
-3 !
-3 .
.
, . , , . name (self ), .
, . , self. . , name self.name . . , , !
. , self.__class__. , self.__class__ .
, . , , classmethod staticmethod . , classmethod .
, .
, (, ) . , @classmethod :
= classmethod()
__init__ . , , 1 . , self.name , .
, self . .
, . .__doc__ , ._.__doc__ .
, . 1 .
. : __ , - .
, , , , / . , ( ).
++//#
, ( ) public , virtual .
, . , .
. , . , , , .
, . .
, , . , () , .
. , . , . , . , , . , , .
, ; , .
, . , .
(_.py ):
class :
''' .'''
def __init__(self, , ):
self. =
self. =
print('( : {})'.format(self.))
def (self):
''' .'''
print(':"{}" :"{}"'.format(self., self.), end=" ")
class ():
''' .'''
def __init__(self, , , ):
.__init__(self, , )
self. =
print('( : {})'.format(self.))
def (self):
.(self)
print(': "{:d}"'.format(self.))
class ():
''' .'''
def __init__(self, , , ):
.__init__(self, , )
self. =
print('( : {})'.format(self.))
def (self):
.(self)
print(': "{:d}"'.format(self.))
= ('. ', 4, 3)
= ('', 25, 75)
#
print()
= [, ]
for in :
#
.()
:
$ python _.py
( : . )
( : . )
( : )
( : )
:". " :"4" : "3"
:"" :"25" : "75"
, (, class ()). , , __init__ self . - __init__ , , .
, __init__ , .
, . . ( ), . , . , . , , . , , ( , ) , .
- , .
() print , , end . \n ( ) .
, . . , .
, / .