Tuesday, June 5, 2018

when no-ops attack VII: assignment's revenge

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"

10 comments:

  1. 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
  2. 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.

    ReplyDelete
  3. Whoi, 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代写

    ReplyDelete
  4. The 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

    ReplyDelete
  5. Your social media profiles need to be informational, exciting and attractive to draw the attention of fans and followers. check over here

    ReplyDelete
  6. This 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

    ReplyDelete
  7. bathroom contractors seattle individuals claiming to speak for Anonymous announced their goals and intentions via a video posted.

    ReplyDelete