Skip to content
Advertisement

Creating and declaring list in class

I am trying to create a list within a class and then declaring elements in that list. I don’t even know if this is the right way for python. I have some java background.

I didn’t add 10 elements in the list manually because I feel creating a dynamic list will be more useful.

JavaScript

Advertisement

Answer

Your problem lies in here.

JavaScript

At this moment, size is empty, it doesn’t contain any elements so why are you checking size[i] == None? You probably think that a python list behaves like an array in Java where it initializes everything with null? Even though here you are not declaring the size of the list inside the init constructor, so I’m curious how you thought of that.

Your code should look like this:

JavaScript

Also a bonus:

JavaScript

Self is the this keyword in python, the difference is that in Python you need to pass it as an argument only when declaring methods, not when you call them and also you can name it whatever you want!

Hope this helps!

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement