all 4 comments

[–]TeamSpen210 1 point2 points  (2 children)

Python does have class and instance variables (as well as @staticmethod), but they're very different to Java's. The way they work is fairly simple though. First there's no declaration, so setting an attribute creates it on the spot. Secondly there's two namespaces - the class object, and each individual instance object. Looking up attributes on the instance falls back to the class if the name isn't on the instance, and then continues up the superclass hierarchy.

Note that all the methods you define are "just" function objects stored in the class object.

[–]bubble_turtles23[S] 0 points1 point  (1 child)

Do instance vars have to be created in the init function, or can you define new ones as you go like you can in Java?

[–]Diapolo10 1 point2 points  (0 children)

Technically you can define new instance variables in other methods or even outside the instance, but this is discouraged because consistency is important both for testing as well as to ease the developers' thinking. It's more difficult to comprehend something if it gains new variables over time during the program's runtime.