1
def __init__(self):
    self.score=[]

def __init__(self,score=[]):
    self.score=score

def __init__(self,score):
    self.score=[]

Can you please explain what happens behind the scenes? TIA.

Prune
  • 76,765
  • 14
  • 60
  • 81
Kaushik
  • 83
  • 5
  • 2
    The third one is somewhat pointless, as it produces the same result as the first. The second is (probably) a [bad idea](https://stackoverflow.com/questions/1132941/least-astonishment-and-the-mutable-default-argument). – chepner Dec 04 '18 at 17:59
  • There are no attribute declarations in Python. In any event, this is irrelevant that they happen to be `__init__` methods, this is just asking what the difference is between different kinds of parameters... – juanpa.arrivillaga Dec 04 '18 at 18:02

5 Answers5

3
def init(self):
    self.score=[]

self.score is always initialized to an empty list.

def init(self,score=[]):
    self.score=score

The caller can pass in an optional list parameter. If they don't, self.score is initialized to an empty list once, when the function is defined. (See Common Gotchas: Mutable Default Arguments for why this is a bad idea.)

def init(self,score):
    self.score=[]

The caller must pass in a list parameter (normally, that would be assigned to self.score, but you're throwing that value out and setting self.score to an empty list). The third example probably should be:

def init(self, score):
    self.score = score
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
  • 1
    "If they don't, self.score is initialized to an empty list." strictly speaking, not true. It is initialized to the object that is the default parameter. It may or may not be an empty list, if that object gets mutated, for example. Mutable default arguments are dangerous if you don't know how they work – juanpa.arrivillaga Dec 04 '18 at 18:08
  • @juanpa.arrivillaga You're right. I always forget about this when the IDE doesn't warn me. I updated my answer. – Bill the Lizard Dec 04 '18 at 18:23
0
  1. The "score" member is always set to []
  2. You have the option to specify what you want to set the member "score" to.
  3. You have to specify "score" while constructing the object. The difference to 2. is, that here you have to specify it, while in 2. it's optional, and you're just setting a default
stekepego
  • 45
  • 1
  • 6
0

In the first one, you always assign [] to the objects score variable. In the second one, you can construct the object with or without the score value, for example if you do obj = object([1,2,3]), then it will have score equal to [1,2,3], but you can also call it without the score argument, like obj = object(), and it will have score equal to []. In the third example, you have to create with the score argument, but it will be ignored, as it is not assigned to the self.score variable.

0

score in function header is a local variable

def init(self,score):

and here it is an instance attribute

self.score=[]
ak_app
  • 170
  • 10
0

The first and last methods have exactly the same effect: assign an empty list to the instance attribute score. For some strange reason, the third one requires an extra positional parameter score, which is not used.

The middle one creates and reserves a default value for the parameter score; at parse time, this value is an empty list. Any instantiation that omits that argument gets assigned the reserved list. The danger here is that these instantiations will all refer to the same list: a change to the score attribute in one will be reflected in all of the other objects that used the default value.

Prune
  • 76,765
  • 14
  • 60
  • 81