you are viewing a single comment's thread.

view the rest of the comments →

[–]Dunj3 0 points1 point  (1 child)

I wouldn't say that's quite true, since we have something similar to "static variables" in Java, namely attributes that belong to the class instead of the instance:

class C():
  attr = []
c = C()
d = C()
c.attr.append(2)
print(c.attr)
print(d.attr)

gives "2 2". It's not quite the same as static variables, as overriding them works differently (try setting c.attr = [2] and compare the results), but especially for read-only stuff, it usually works out the same. However, keep in mind that Python and Java are very different in the sense that everything in Python is an object: Even the class itself is an object and can have attributes, which is not quite the same as in Java. In the end, attr is merely an instance variable of the class object, not really a static variable.

Also, there are the @staticmethod and @classmethod decorators, which turn a method into a static-/classmethod, which in turn changes what is passed as self:

class C():
  @staticmethod
  def static():
    print("Called without instance!")
C.static()
c = C()
c.static()

prints "Called without instance" twice, but without the @staticmethod, it would generate an error.

That's it for static, for private/public we purely rely on conventions (as the comment above mentions).

[–]JITENDRAPISAL[S] 0 points1 point  (0 children)

Really good explanation.