isinstance (obj, cls) determines whether an object obj is an instance of a derived class of cls
Whether issubclass (sub, super) determines sub class is a subclass of the super class
__getattr__ property access does not exist, triggering __getattr__
__getattribute__ regardless of whether there is a property will trigger __getattribute__
__setitem__, __delitem__, __getitem__, by brackets [] dictionary manner as in Example del f1 trigger [ 'name']
__setattr__, delattr__, __getattr__, through point the way to trigger such as del f1.name
### __ str__, __repr__ all control output, the output return value can return a string, the string is not a non-return
class Foo:
def __str__(self):
return "customized 1" # can print control information
f1= Foo()
print (f1) # essentially str (f1), f1 actually called .__ str __ ()
>>> custom 1
class Foo:
def __init__(self,name,age)
self.name=name
self.age=age
def __repr__(self):
return "name is% s age% s"% (self.name, self.age) #__repr__ trigger interpreter
f1 = Foo ( 'egon', 19) # trigger __repr__
print(f1)
>>> name egon age 19
# When __str__, __repr__ print priority calls coexistence __str__, if no __str__ then transferred __repr__
### format custom formatting methods
#####One
x = '{0}: {0} - {0}'. format ( 'dog') # digital representation format corresponding to the position in the string
print(x)
>>>dog:dog-dog
#####two
class Date:
def __init__(self,year,mon,day):
self.year=year
self.mon = my
self.day=day
d1=Date(2016,9,27)
x="{0.year}{0.mon}{0.day}".format(d1)
y='{0.year}:{0.mon}:{0.day}'.format(d1)
z='{0.mon}-{0.day}-{0.year}'.format(d1)
print(x)
print (y)
print (z)
>>>
20161226
2016:12:26
12-26-2016
##### Supplement
if not 0:
print("ok")
>>>ok
if not 1:
print("ok")
>>>
#####three
format_dic={
'ymd':'{0.year}{0.mon}{0.day}',
'm-d-y':'{0.mon}-{0.day}-{0.year}',
'y:m:d':'{0.year}:{0.mon}:{0.day}'
}
class Date:
def __init__(self,year,mon,day):
self.year=year
self.mon = my
self.day=day
def __format__(self, format_spec):
Print ( ' I perform it ' )
print('--->',format_spec)
if not format_spec: # # This determines if not absent when format_spec
= format_spec 'YMD' # format_spec When not present, the default value set here
fm=format_dic[format_spec]
return fm.format(self)
d1=Date(2016,12,26)
format (d1) # calls d1 .__ format__
print(format(d1))
>>>
I perform it # execution format (d1)
--->
I perform it # execute print (format (d1))
--->
20161226
##### Fourth, when the key value added format_spec's not in the dictionary or format_spec not in format_dic:
def __format__(self, format_spec):
Print ( ' I perform it ' )
print('--->',format_spec)
if not format_spec or format_spec not in format_dic:
format_spec='ymd'
fm=format_dic[format_spec]
return fm.format(self)
d1=Date(2016,12,26)
print(format(d1,'y:m:d'))
print(format(d1,"ajkdla"))
>>>
I la # perform execution Print (the format (D1, 'Y: m: D' ))
---> y:m:d
2016:12:26
I perform it # execution Print (format (d1, "ajkdla" )), ie, when the key is not in the dictionary's value format_spec
---> ajkdla
20161226
######## __ slots__: To save memory usage, additional restrictions defined attributes that can only be used in __slots__ good key to customized assignment
__slots__:,,,,()
Why __slots__: the dictionary takes up a lot of memory, if you have a few attributes of the class, but there are many examples, in order to save memory can be used to replace __slots__ instance __dict__
class Foo:
__slots__ = ['name','age']
def xyy(self):
pass
f1=Foo()
f1.name='xyy'
f1.age=18
### __ doc__ can not inherit the property, does not define the automatic assignment None
class Foo:
" I'm a girl addicted to learning "
pass
f1=Foo()
print(f1.__doc__)
>>> I'm a girl addicted to learning
### __ class__, __module__ view the class name and module name attribute belongs
### __ del__ triggers run __del__ when trying to delete attributes with del
### __ call__ brackets behind the object, triggering __call__ execution.
class Foo:
def __call__(self):
Print ( " I run " )
f1=Foo()
f1 () # call Foo at __call__
Foo () # call is *** at __call__ , because everything is an object
### iterator protocol: the object becomes the object of the iterative method, the __iter__ added in the class, and call return value __next__
class Foo:
def __init__(self,n):
self.n=n
def __iter__(self):
return self
def __next__(self):
if self.n == 15:
The raise the StopIteration ( "n- maximum 15" )
self.n+=1
return self.n
f1=Foo(10)
Print (F1 .__ Next __ ()) # F1 () i.e. target brackets and Foo class have the __iter __ () to become the iterator, with __next__ return value
print(next(f1))
for I in F1: #for cycle follows the iterator protocol i.e. obj = iter (f1), f1 .__ iter __ ()
Print (i) # equivalent to calling __next __ ()
#### variable value assignment
a=2
b=1
c, d = b, a # b that is assigned to c, a is assigned to d
print(c,d)
>>>1 2
Iterator ### by the number of columns output Feibonaci
class Foo:
def __init__(self,x=0,y=1):
self.x=x
self.y = y
def __iter__(self):
return self
def __next__(self):
self.x,self.y=self.y,self.x+self.y
if self.y>=100:
The raise the StopIteration ( "100 Feibonaci number of columns within " )
return self.y
f1=Foo()
for i in f1:
print(i)