Skip to content

Instantly share code, notes, and snippets.

@geoffreygarrett
Last active December 2, 2019 16:17
Show Gist options
  • Save geoffreygarrett/d163a0eefe3ac033b124f359bb17cabb to your computer and use it in GitHub Desktop.
Save geoffreygarrett/d163a0eefe3ac033b124f359bb17cabb to your computer and use it in GitHub Desktop.
Conditionally created default arguments in Python class instantiation using "or".
class Test:
def __init__(self, arg=None):
self.test_arg = arg or "Default arg"
test1 = Test()
test2 = Test("Not Default arg")
print(test1.test_arg)
# Default arg
print(test2.test_arg)
# Not Default arg
@geoffreygarrett
Copy link
Author

geoffreygarrett commented Dec 2, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment