Skip to content

Instantly share code, notes, and snippets.

@lsloan
Last active July 2, 2016 15:08
Show Gist options
  • Save lsloan/cdfa7953c231b3283c88 to your computer and use it in GitHub Desktop.
Save lsloan/cdfa7953c231b3283c88 to your computer and use it in GitHub Desktop.
Python: A basic implementation of `__new__()` that invokes the same method in the superclass.
"""
Whenever I want to implement my own __new__() for a class, I've usually forgotten
the incantation for calling the method in the superclass, so I'm making note of it
here.
"""
class Fubar(object):
def __new__(cls, *args, **kwargs):
"""
To use, replace `Fubar` in the call to `super()` with the name of this class.
"""
instance = super(Fubar, cls).__new__(cls, *args, **kwargs)
# You code here...
return instance
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment