Last active
September 26, 2018 14:02
-
-
Save jmichalicek/00cf692c056d3ec2c1e15ab3c334bc25 to your computer and use it in GitHub Desktop.
Simple example of what Python's Graphene library does
This file contains hidden or 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
""" | |
This is an example of how Python's Graphene library behaves. This can result in `self` not being what you expect, particularly | |
when using graphene-django and `self` on your graphene resolver object is the underlying django model | |
""" | |
class A: | |
def foo(self): | |
print("This is the foo() method on an object of class", type(self)) | |
class B: | |
pass | |
class C: | |
def do_things(self): | |
print("C() objects do not have a foo() method but I am about to call self.foo()") | |
self.foo() | |
type_a = A() | |
type_b = B() | |
A.foo(type_b) | |
C.do_things(type_a) | |
""" | |
Usage/output | |
>>> type_a = A() | |
>>> type_b = B() | |
>>> | |
>>> A.foo(type_b) | |
This is the foo() method on an object of class <class '__main__.B'> | |
>>> C.do_things(type_a) | |
C() objects do not have a foo() method but I am about to call self.foo() | |
This is the foo() method on an object of class <class '__main__.A'> | |
>>> A.foo(type_a) | |
This is the foo() method on an object of class <class '__main__.A'> | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment