Skip to content

Commit 45d3b7e

Browse files
committed
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.
1 parent ad4fb2f commit 45d3b7e

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

python.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -856,13 +856,16 @@ class Superhero(Human):
856856
# This constructor inherits the "name" argument from the "Human" class and
857857
# adds the "superpower" and "movie" arguments:
858858
def __init__(self, name, movie=False,
859-
superpowers=["super strength", "bulletproofing"]):
860-
859+
superpowers=None):
861860
# add additional class attributes:
862861
self.fictional = True
863862
self.movie = movie
863+
864864
# be aware of mutable default values, since defaults are shared
865-
self.superpowers = superpowers
865+
if superpowers is None:
866+
self.superpowers = ["super strength", "bulletproofing"] # Default superpowers, but this way new list is created on each initialization.
867+
else:
868+
self.superpowers = superpowers
866869

867870
# The "super" function lets you access the parent class's methods
868871
# that are overridden by the child, in this case, the __init__ method.

0 commit comments

Comments
 (0)