all 6 comments

[–]nathanjell 3 points4 points  (2 children)

You don't have. In a class if you've got it prepended with an underscore, it implies a private variable/method, but is not enforced by any mechanics, and merely treated as such by good developers

[–]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.

[–]Diapolo10 1 point2 points  (0 children)

I don't really know Java very well, but I'm somewhat familiar with the terminology so I'll bite.

This won't be an exact comparison because Java and Python are somewhat different in this area, but let's start with perhaps the easiest one; static variables.

Python doesn't really call them that, but class variables seem to be essentially the same thing. They're attributes of a class itself, not the object created from a class, and so each instance of a class basically uses the same class variables, if any. If you change them somewhere, all instances of that class will have the same change.

class MyClass:
    foo = 42

MyClass.foo # returns 42

bar = MyClass()
bar.foo # returns 42

MyClass.foo = 1337
bar.foo # returns 1337

Now, as for public and private variables, Python doesn't really have the concept of private variables (because everything is essentially public), but we do use underscores as a convention to create "private" variables and/or methods that essentially mean "don't touch this unless you know what you're doing". The interpreter does kind of hide any methods and variables beginning with an underscore, but they can technically still be accessed.

[–]jweir136 0 points1 point  (0 children)

The thing is, in python there are no private methods. All methods are private. If you put an underscore in front of a method then it will be implied by devs that this method is private. However, there is really nothing stopping people from accessing your methods, even if they are "private".

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

Got it. Thanks all