Skip to content

Instantly share code, notes, and snippets.

@christianscott
Last active November 7, 2017 08:17
Show Gist options
  • Save christianscott/5f8a8324e03caf3e360f67904eed2801 to your computer and use it in GitHub Desktop.
Save christianscott/5f8a8324e03caf3e360f67904eed2801 to your computer and use it in GitHub Desktop.
Poor man's prototypal inheritance in Python
class Object:
def __init__(self, *parents):
self.parents = [parent() for parent in parents]
def __getattr__(self, name):
for parent in self.parents:
if hasattr(parent, name):
return getattr(parent, name)
raise AttributeError(f"'Object' object has no attribute '{name}'")
def Parent():
this = Object()
this.foo = "foo"
return this
def Child():
this = Object(Parent)
this.bar = "bar"
def say_hello(name):
return f"Hello, {name}!"
this.say_hello = say_hello
return this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment