In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces the "brief introduction of python descriptors". In daily operation, I believe many people have doubts about the brief introduction of python descriptors. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "brief introduction of python descriptors"! Next, please follow the editor to study!
What is a descriptor?
The python descriptor is an object property of a "binding behavior", and in the descriptor protocol, it can override access to the property through methods. These methods are get (), set (), and _ _ delete__ (). If any of these methods are defined in an object, then the object is a descriptor.
In other words: an object that contains one of the following three methods is a descriptor
Three methods (protocols):
Get (self, instance, owner): triggers when a property is called
Set (self, instance, value): triggers when a property is assigned a value
Delete (self, instance): triggers when an attribute is deleted with del
What is self instance, owner?
Self is the current descriptor (object) itself, similar to the C++this pointer, instance is the class object entity to which the descriptor belongs, and owner is the class in which the descriptor is located (who owns these things is, of course, the highest class)
Class Desc (object):
Def _ _ get__ (self, instance, owner):
Print ("_ _ get__...")
Print ("self:\ t\ t", self)
Print ("instance:\ t", instance)
Print ("owner:\ t", owner)
Print ('='* 40, "\ n")
Def _ _ set__ (self, instance, value):
Print ('_ _ set__...')
Print ("self:\ t\ t", self)
Print ("instance:\ t", instance)
Print ("value:\ t", value)
Print ('='* 40, "\ n")
Class TestDesc (object):
X = Desc ()
# the following is the test code
T = TestDesc ()
T.x
# the following is the output information:
_ _ get__...
Self:
Instance:
Owner:
= =
@ property disguises a function call as an access to a property
Class Foo:
@ property
Def attr (self):
Print ('getting attr')
Return 'attr value'
Def bar (self): pass
Foo = Foo ()
X = property (get, set, del)
Container type protocol:
If you want the custom container to be immutable: you only need to define the magic of len () getitem ()
If you want the customized container to be changeable, you also need to define _ _ setitem__ delitem _ ().
Class CountList:
Def _ _ init__ (self, * args):
Self.values = [x for x in args] # list derivation
Self.count = {} .fromkeys (range (len (self.values)), 0) # set up a dictionary to record the number of visits
# dictionary; 0-len (self.value)
Def _ len__ (self):
Return len (self.values)
# get the value, not the number of visits
# gwtitem, as long as access is bound to trigger, this is magic
Def _ getitem__ (self,key):
Self.count [key] + = 1
Return self.values [key]
Python iterator: iter next
String = 'hello, python'
It = iter (string)
Next (it)
Next (it)
...
Throw an exception when the iter iterator is false
It = iter ("hello,world")
While True:
Try:
Each = next (it)
Print (each)
Except:
Break
The magic method of iterator:
Iter ()
Next ()
Class Iter:
Def _ _ init__ (self, n = 100):
Self.a = 0
Self.b = 1
Self.n = n
Def _ iter__ (self):
Return self
Def _ next__ (self):
Self.a, self.b = self.b, self.a + self.b
If self.a > self.n: # stop iteration
Raise StopIteration
Return self.a
Generators: functions of yeild statements
The generator is also an iterator: the yield statement returns
Random entry: Hu Jinli, no rule, hey
Collaborative program
A collaborative program is an independent function call that can be run, which can be paused or suspended and continue to execute from where the program leaves when needed.
A generator is also an iterator, so it can be used with next (iterator object), just like an iterator.
Watch carefully: yield returns after the first call of the generator generator, and then does not start at the beginning of the function, but continues to call from the place where it left
Def generator (): http://www.xadsyyfk.com/ of Zhengzhou Gynecology Hospital
A = 0
B = 1
Print ("start iteration generator")
While True:
A, b, b
Yield a
For each in generator ():
If (each > 100):
Break
Print (each,end ='') # do not wrap lines, replace line breaks with spaces
The generator derivation can be used as an argument to a function without ()
Eg: 0 +... + 9
Originally: sum ((i for i range (10))
Harmony off () sum sum (i for i in range (10))
List derivation [i for i in range (10)]
Dictionary derivation {i for i in range (100)}
There is no tuple deduction Oh, it becomes the generator generator deduction
Module
Containers: encapsulation of data
Functions: encapsulation of statements
Classes: encapsulation of methods and properties
Module: it is the program.
Import module name:
Find1:
Import: import + module name (as t)
Call: module name (t). + function
Name = = main
If name = = main function: let python identify whether the program is running in the main program or in the module: that is, let python identify whether it is a private program or a public program
That is, if name = = main is the main program? If so, what do you do? if not, what do you do?
Search path
The process of importing python module has a process of path search.
Sys.path.append first ("module path")
Re-import module
Package package
The first step is to create a folder storage module. The name of the file is the name of the bag.
The second step is to create a _ _ init__.py module file in the folder. The content can be empty.
The third step is to put the relevant modules in the folder (package)
Import: package name. Module name
At this point, the study of "A brief introduction to python descriptors" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.