Last active
December 14, 2015 21:49
-
-
Save DRMacIver/5154208 to your computer and use it in GitHub Desktop.
Apparently I don't understand python method invocation at all.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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() | |
p = Bar().print3 | |
p() | |
""" | |
Prints: | |
(<__main__.Bar instance at 0x7f109c35ecf8>,) | |
(<__main__.Printer instance at 0x7f109c35ecb0>,) | |
(<__main__.Bar instance at 0x7f0b32bd7dd0>,) | |
(<__main__.Bar instance at 0x7f379f67ddd0>,) | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment