In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what are the object methods of the Python class". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the object methods of the Python class".
Today, I looked at the decorators classmethod and staticmethod, confused myself, and then sorted out the results of the above methods called by classes and objects:
One thing I'm not sure about is what method staFun () belongs to? I call it a class function, because I don't know what it is right now, but what's certain is that
StaFun is not an ordinary method (object method) because an object cannot be called.
StaFun is not a static method, because the object cannot be called, the static method of the class, the object of the class can also be called.
StaFun cannot be called a class method either, because objects can also call class methods decorated with classmethod decorators.
So staFun can only be an ordinary function of a class.
Class Super (): name = 'super Man' # object method of bound method (common method) def objFun (self): print (self) # class function of bound method this seems to be rarely used in the class This ignores def staFun (): print ('staFun') # class methods of bound methods @ classmethod def clsFun (cls): print (cls) # static methods of unbound methods (as ordinary functions) @ staticmethod def staticFun (): Print ('staticFun') obj = Super () # object invokes the object method obj.objFun () print (' binding relationship: {} '.format (obj.objFun))''# the default parameter is the current object binding relationship: # bind to the method on the object''# Class calls the object method try: Super.objFun () except Exception as e: Print ('class cannot call object methods Missing instantiated object parameter') print ('can also be called if the instantiated object is used as an argument But this is meaningless') # object calls class method try without decorator: obj.staFun () except Exception as e: print ('object cannot call class method without classmethod decorator') # class calls class method Super.staFun () print (Super.staFun) 'staFun # normal call # as a function call '' # object calls decorator class method obj.clsFun () print ('binding relationship:% s'% obj.clsFun)''# object calls decorator class method by default or class # method bound to class''# class calls decorator class method Super.clsFun () print ('binding relationship:% s' % Super.clsFun)''# Class calls class methods with decorator default pass parameter is class # method bound to class''object calls staticmethod decorated static method obj.staticFun () print (obj.staticFun)' 'staticFun # normal call # as a function call class calls staticmethod decorated static method Super.staticFun () print (Super.staticFun)''staticFun # normal call # as a function call' 'Thank you for reading The above is the content of "what are the object methods of the Python class?" after the study of this article, I believe we have a deeper understanding of what the object methods of the Python class have, and the specific use needs to be verified in 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.