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"

15 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. bathroom contractors seattle individuals claiming to speak for Anonymous announced their goals and intentions via a video posted.

    ReplyDelete
  7. A round of applause for your mind blowing article. Much thanks to you, Fantastic.
    whirlpool 9 kg semi automatic top load washing machine price

    ReplyDelete
  8. This programming language is most wanted and many professionals want to learn it because they want to earn a lot of money and make their future bright. Dissertation proposal writing services.

    ReplyDelete
  9. It was a very good post indeed. I thoroughly enjoyed reading it in my lunch time. Will surely come and visit this blog more often. Thanks for sharing. 경마

    ReplyDelete
  10. That was an excellent article. You made some great points and I am grateful for for your information! Take care! 바카라사이트

    ReplyDelete
  11. This content is simply exciting and creative. I have been deciding on a institutional move and this has helped me with one aspect. tourism assignment help

    ReplyDelete
  12. Positive site, where did u come up with the information on this posting?I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work. 메이저사이트

    ReplyDelete
  13. We are one of the best web-based assignment assist with adjusting suppliers, with superb quality and reasonable expenses, having provided great many fruitful duplicates. Assignment Help

    ReplyDelete
  14. Thanks for sharing the solution, when I was in college and stuck with some assignments then I used to get assignment help from Unifolks. They have an amazing team who provide you with well-written assignments.

    ReplyDelete