Let's define a very simple class:
>>> class F(object):
... @staticmethod
... def f(): return "I'm such a simple function, nothing could go wrong"
...
>>> F.f()
"I'm such a simple function, nothing could go wrong"
Now, let's do a trivial no-op to this class:
>>> F.f = F.f
Surely nothing changed, right?
>>> F.f()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unbound method f() must be called with F instance as first argument (got nothing instead)
What happened? staticmethod uses the descriptor protocol in order to return something other than itself when accessed as an attribute. The assignment above is not a no-op, because it is not setting the value back to what it already was, but to what was returned by __get__ of the staticmethod object.
>>> class F(object):
... @staticmethod
... def f(): return "I'm not what I seem"
...
>>> F.f
<function f at 0x7f05eda596e0>
>>> F.__dict__['f']
<staticmethod object at 0x7f05eda5ce50>
Version note -- Python3 doesn't raise an exception, although the type still changes from staticmethod to function.
>>> class F:
... @staticmethod
... def f(): return "I'm protected by python3 wizardry"
...
>>> F.f()
"I'm protected by python3 wizardry"
>>> F.__dict__['f']
<staticmethod object at 0x7fd087b739b0>
>>> F.f = F.f
>>> F.__dict__['f']
<function F.f at 0x7fd087b5cae8>
>>> F.f()
"I'm protected by python3 wizardry"
Thank you because you have been willing to share information with us. we will always appreciate all you have done here because I know you are very concerned with our. 澳洲代写
ReplyDelete
ReplyDeleteNice post,thanks for posting
Python training course in delhi
Python training course in Noida
Amazing Article, Really useful information to all So, I hope you will share more information to be check and share here.
ReplyDeletePygame Tutorial
Pygame Download
Pygame Install
Matplotlib Python
Matplotlib Tutorial
Matplotlib install
PouchDB Tutorial
What is PouchDB
PouchDB Installation
pouchdb server
Super article
ReplyDeleteWhat is Cyber Security
Types of Cyber Attacks
Types of Cyber Attackers
Cyber Security Technology
Cyber Security Tools
Cyber Security Standards
What is Google Adwords
Google Adwords tutorial
Google Keyword Planner
How to Advertise on Google
Online high school Math tutoring is a good resource for students who struggle with Trusted PhD Thesis Writing Service their assignments. The tutors help students do complicated sums on the white board and correct them extempore.
ReplyDeleteWhoi, excellent, t wondered just how to cure icne. ind found your webstte by google, dtscovered todiy t'm i ltttle obvtous i greit deil. t’ve ilso idded RSS ind sive your webstte. keep us updited. assignment代写
ReplyDeleteThe odds of the above situation happening are little. Nonetheless, the bigger your judgment is, and the bigger the resources your judgment indebted person procures, wins, or acquires, the greater possibility that a hanging assignment of judgment could abruptly be documented. how to write a reflection paper
ReplyDeleteYour social media profiles need to be informational, exciting and attractive to draw the attention of fans and followers. check over here
ReplyDeleteThis information is impressive; I am inspired how continuously you describe this topic. After reading your post, thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic snowflake data warehouse
ReplyDeletebathroom contractors seattle individuals claiming to speak for Anonymous announced their goals and intentions via a video posted.
ReplyDelete