Skip to content

Instantly share code, notes, and snippets.

@public
Forked from DRMacIver/method_invocation.py
Created March 13, 2013 17:27
Show Gist options
  • Save public/5154306 to your computer and use it in GitHub Desktop.
Save public/5154306 to your computer and use it in GitHub Desktop.
# Python method invocation is a strange beast
# Note difference between assigning a callable object that isn't a function
# and a callable object that is one
class Printer:
def __call__(*args):
print(args)
def print_stuff(*args):
print(args)
class Bar:
def print1(*args):
print(args)
print2 = Printer()
print3 = print_stuff
Bar().print1()
Bar().print2()
Bar().print3()
"""
Prints:
(<__main__.Bar instance at 0x7f109c35ecf8>,)
(<__main__.Printer instance at 0x7f109c35ecb0>,)
(<__main__.Bar instance at 0x7f0b32bd7dd0>,)
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment