Skip to content

Instantly share code, notes, and snippets.

@ranchodeluxe
Last active August 29, 2015 14:00
Show Gist options
  • Save ranchodeluxe/11195555 to your computer and use it in GitHub Desktop.
Save ranchodeluxe/11195555 to your computer and use it in GitHub Desktop.
i like python that acts like javascript
class A( object ):
def bang(self):
print "bang"
print "self => %s" % self
return True
class B( object ):
pass
if __name__ == '__main__':
b = B()
unbound_func = getattr( A, 'bang' )
print unbound_func
bound_func = unbound_func.__get__( b, A )
print bound_func
print bound_func()
-----------OUTPUT------------------
<unbound method A.bang>
<bound method A.bang of <__main__.B object at 0x213fcd0>>
bang
self => <__main__.B object at 0x213fcd0>
True
@mattmakesmaps
Copy link

Starring, as the change from "go" --> "bang" makes this example much more clear to the end-user ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment