mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-04-26 15:13:56 +00:00
Update python.md
Be more explicit to assign default lists to a new python instance. Defaults are already mentioned, but this code is safer to avoid having different instances pointing to the very same list without intention.
This commit is contained in:
parent
ad4fb2fc58
commit
7f6d36839e
@ -856,13 +856,16 @@ class Superhero(Human):
|
||||
# This constructor inherits the "name" argument from the "Human" class and
|
||||
# adds the "superpower" and "movie" arguments:
|
||||
def __init__(self, name, movie=False,
|
||||
superpowers=["super strength", "bulletproofing"]):
|
||||
|
||||
superpowers=None):
|
||||
# add additional class attributes:
|
||||
self.fictional = True
|
||||
self.movie = movie
|
||||
# be aware of mutable default values, since defaults are shared
|
||||
self.superpowers = superpowers
|
||||
if superpowers is None:
|
||||
# assigns default superpowers, but this way new list is created on each initialization.
|
||||
self.superpowers = ["super strength", "bulletproofing"]
|
||||
else:
|
||||
self.superpowers = superpowers
|
||||
|
||||
# The "super" function lets you access the parent class's methods
|
||||
# that are overridden by the child, in this case, the __init__ method.
|
||||
|
Loading…
Reference in New Issue
Block a user