In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to use getattr,__getattr__,__getattribute__ and _ _ get__ in Python. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
Getattr
Getattr (object, name [, default]) is one of the built-in functions of Python, which is used to get the properties of an object.
Example
> class Foo:... Def _ _ init__ (self, x):... Self.x = x... > > f = Foo (10) > getattr (f,'x') 10 > f.x10 > getattr (f, 'yearly,' bar') 'bar'__getattr__
Object.__getattr__ (self, name) is an object method that is called when the property of the object is not found.
Example
> class Frob:... Def _ _ init__ (self, bamf):... Self.bamf = bamf... Def _ _ getattr__ (self, name):... Return 'Frob does not have `{}` attribute.'.format (str (name))... > > f = Frob ("bamf") > f.bar'Frob does not have `bar` attribute.' > f.bamf'bamf'getattribute
Object.__getattribute__ (self, name) is an object method that is called unconditionally when accessing the properties of an object. This method should return a property value or throw an AttributeError exception.
Example
Class Frob (object):... Def _ _ getattribute__ (self, name):... Print "getting `{}`" .format (str (name)).. Return object.__getattribute__ (self, name)... > > f = Frob () > > f.bamf = 10 > f.bamfgetting `bamf`10get
The _ _ get__ () method is one of the descriptor methods. The descriptor is used to transform access object properties into calling descriptor methods.
Example
Class Descriptor (object):... Def _ _ get__ (self, obj, objtype):... Print ("get value= {}" .format (self.val)). Return self.val... Def _ _ set__ (self, obj, val):... Print ("set value= {}" .format (val)). Self.val = val... > > class Student (object):... Age = Descriptor (). > > s = Student () > > s.age = 12set value=12 > print (s.age) get value=1212 on "how to use getattr,__getattr__,__getattribute__ and _ _ get__ in Python" is here. I hope the above content can be of some help to you, so that you can learn more knowledge. If you think the article is good, please share it out for more people to see.
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.