In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains the "python parameters + keyword parameters + inspect module example analysis", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "python parameters + keyword parameters + inspect module example analysis" bar!
Function introspection (function introspection)
In addition to the _ _ doc__ attribute, the function object has many properties. For the following functions, you can use dir () to view the properties that the function has:
> dir (factorial) ['_ _ annotations__','_ _ call__','_ _ class__','_ _ closure__','_ _ code__','_ _ defaults__','_ _ delattr__','_ _ dict__','_ dir__','_ doc__','_ eq__','_ format__','_ _ ge__','_ _ get__' '_ _ getattribute__',' _ _ globals__','_ _ gt__','_ _ hash__','_ _ init__','_ _ kwdefaults__','_ _ le__','_ _ lt__','_ _ module__','_ _ name__','_ ne__','_ new__','_ qualname__','_ _ reduce__' '_ _ reduce_ex__',' _ _ repr__','_ _ setattr__','_ _ sizeof__','_ _ str__','_ _ subclasshook__']
Most of these are properties that are common to Python regular classes. Let's focus on the properties that regular objects do not have but function objects have:
> class dir passthrough. > obj = C () > def func (): pass... > sorted (set (dir (func))-set (dir (obj) # calculate the difference Then sort ['_ _ annotations__','_ _ call__','_ _ closure__','_ _ code__','_ _ defaults__','_ _ get__','_ _ globals__','_ _ kwdefaults__','_ _ name__','_ _ qualname__']
The function-specific properties listed above are described as follows:
Comments for _ _ annotations__ dict parameters and return values
_ _ call__ method-wrapper implements the () operator, which calls the object's protocol
The _ _ closure__ tuple function closure, that is, the binding of free variables (usually None)
_ _ code__ code compiled into bytecode function metadata and function definition body
Default value of _ _ defaults__ tuple form parameter
_ _ get__ method-wrapper implements read-only descriptor protocol
Global variables in the module where the _ _ globals__ dict function is located
_ _ kwdefaults__ dict is limited to the default values of keyword form parameters only
_ _ name__ str function name
Qualified name of _ _ qualname__ str function
Location parameter and keyword parameter def tag (name,*content,cls=None,**attrs): if cls is not None:attrs ['class'] = clsif attrs:attrs_str =' .join ('% s''% (attr,value) for attr,value in sorted (attrs.items () else:attrs_str=''if content:return'\ n'.join ('% s'% (name,attrs_str,c) Name) for c in content) else:return''% (name,attrs_str) print (tag ('br')) # Positioning parameter nameprint (tag (' packs) hello.') # hello will be captured by * conteng and stored in the tuple content = ('hello') print (tag (' pawns) print (tag ('packs) print') Id=33) # attrs= {'id':33} content= (' hello') print (tag)) # cls keyword is passed into cls='sidebar'print (tag (content='testing',name='img')) # the first parameter name can also be passed as a keyword # the same name key will be bound to the corresponding named parameter The rest will be captured by * * attrs print (tag (* * {'name':'img','title':'sunset boulevard','src':'sunset.jpg','cls':'framed'})) # keyword parameter is a new feature of python3.0. In the above example, cls parameter can only be specified by keyword parameter, it will not capture unnamed positioning parameter # when defining function, if you want to specify keyword parameter only To put them after the parameters of * def f (a recorder): return a dagger bff = f (1 meme bau2) print (ff) hello
Hello
World
Hello
Hello
World
(1, 2) inspect template def tag (name,*content,cls=None,**attrs): if cls is not None:attrs ['class'] = clsif attrs:attrs_str =' .join ('% s "'% (attr,value) for attr,value in sorted (attrs.items () else:attrs_str=''if content:return'\ n'.join ('% s'% (name,attrs_str,c,name) for c in content) else:return'% (name) Attrs_str) import inspectsig = inspect.signature (tag) print (sig) my_tag = {'name':'img','title':'sun long','src':'sunlong.jpg','cls':'framed'} bound_args = sig.bind (* * my_tag) for name,value in bound_args.arguments.items (): print (name,'=',value) print (bound_args)
The inspect module binds arguments to function calls:
(name, * content, cls=None, * * attrs) name = imgcls = framedattrs = {'title':' sunlong', 'src':' sunlong.jpg'} Thank you for reading. The above is the content of "python parameters + keyword parameters + inspect module example analysis". After the study of this article, I believe you have a deeper understanding of the problem of python parameters + keyword parameters + inspect module example analysis. The specific use situation still needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.