0

enter image description here

From my understanding, the inbound_nodes=[] passed to the init just declares an empty list. So even if I create a subclass of Node and in that subclass call this init function with no arguments passed, the __init__function would still work, as it declared an empty list itself.

Am I right?

The next question is, we can see that self.inbound_nodes = inbound_nodes.

Does it mean, that self.inbound_nodes is also an empty list but inside the Node superclass? If so, why can't I just declare self.outbound_nodes in the same way?

And the last question is, what does the last for loop does?

  • Python does not have variable declarations, you are *initializing* an empty list, using a list literal syntax, then *setting that as a default parameter*. You aren't *passing anything*, that is a *function definition*. Note, this is a source of common bugs, if you do not understand the way python default parameters work, see [this famous question](https://stackoverflow.com/questions/1132941/least-astonishment-and-the-mutable-default-argument) – juanpa.arrivillaga Sep 16 '17 at 21:35
  • 1
    Also, `self.inbound_nodes = inbound_nodes` creates an instance variable `self.inbound_nodes` and assigns it to the *same object* as `inbound_nodes`. You should really just read a tutorial on OOP in Python. Also, please do not *post pictures of code!*. Code is text. Please post your code as formatted text *in the question itself*. – juanpa.arrivillaga Sep 16 '17 at 21:37
  • the inbound_nodes=[] argument is a default value, which means if the caller call the function with a list it is ok and if not it will pass an empty list – Fady Saad Sep 16 '17 at 21:41
  • 1
    Another good read on [mutable default arguments and why you need to be careful to understand how they work in Python](http://docs.python-guide.org/en/latest/writing/gotchas/#mutable-default-arguments) – juanpa.arrivillaga Sep 16 '17 at 21:46
  • It is preferable to post the actual code/data instead of an image of the code/data. – wwii Sep 16 '17 at 21:48
  • 1
    One nice thing about Python being an interpreted language is that you can have a quick and easy write-test-modify-test-... cycle. You can play around with that class in the shell and probably get a good idea of the answers to all your questions. – wwii Sep 16 '17 at 21:51

1 Answers1

1

Not exactly. When parameters are initialized at the function definition, they are only executed once. It will create an empty list and use that as a default argument. That means, all the objects which were initalized with the default parameters use the same list

class Node(object):
    def __init__(self, inbound=[]):
        self.inbound = inbound

class SubNode(Node):
    pass

node = Node()
subnode = SubNode()

print node.inbound is subnode.inbound  # returns True
print id(node.inbound)  # 66058248
print id(subnode.inbound)  # 66058248

It's the same list object. That's because function parameters are evaluated only once. If you wanted each object has its own empty list, you should define the init as follows

def __init__(self, inbound=None):
        self.inbound = inbound or []

Regarding the loop, I'm not sure what it is suppose to do.

Chen A.
  • 10,140
  • 3
  • 42
  • 61
  • So, for example, let's say that I have created a new sublcass called A: `class A(Node)` So in my subclass A, I wrote this code: `class A(Node): def __init__(self): Node.___init___(self)` Does it mean, that because I did not pass any values, by default, class A will get self.inbound = []? –  Sep 16 '17 at 22:21
  • You don't need to call `Node.__init__(self)` as it happens by default if you don't override it. And yes, it will have the same list object. Think about it that way, the function is evaluted once and a default list object is created. When the function is called (object creation) that list object is assigned unless another object is provided. – Chen A. Sep 16 '17 at 22:32
  • @AbylIkhsanov if you found my answer helpful, please consider upvoting it. thanks – Chen A. Sep 17 '17 at 07:06