Skip to content

Instantly share code, notes, and snippets.

@fsword
Created July 23, 2014 06:27
Show Gist options
  • Save fsword/0f9305ac87466142bbbc to your computer and use it in GitHub Desktop.
Save fsword/0f9305ac87466142bbbc to your computer and use it in GitHub Desktop.
sample code to explain the fail design of default argument support in python
class Member:
def __init__(self,name='',group=''):
self.name = name
self.group = group
class Team:
def __init__(self, names):
self.members = [Member(n,str(hash(self))) for n in names]
def join_us(self,member=Member()):
self.members.append(member)
member.group = str(hash(self))
return member
def print_members(self):
for n in self.members:
print n.name+"\t"+n.group
dev_team = Team(['john'])
test_team = Team(['smith','tom'])
tom = test_team.members[1]
dev_team.join_us(tom)
new_guy = dev_team.join_us()
new_guy.name = 'xman'
another_new_guy = dev_team.join_us()
another_new_guy.name = 'jim'
dev_team.print_members()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment