Sorry for the poor title. I really have no idea how to describe this to a search engine to find out how it works.
class MyClass(object):
def __init__(self, contents=None):
self.contents = contents
Specifically, the contents=None parameter.
I’ve been studying Python for about 2 months now, and that part semi-blows my mind. Any help or redirection to a similar, previously asked question will be very appreciated.
That’s a Default Argument Value for a Keyword Argument.
They’re pretty straightforward, as long as they’re not a mutable object like a list or dictionary.
Just in case it was the
__init__method throwing you off — it’s the instance method of a class that gets automatically called when the instance is created. Any arguments you pass when you call the class to create a new instance, likeor
in your example, are passed to the
__init__method.