member or class variables in python
python, variables
Solution
class Sample(object):
x = 100
_a = 1
__b = 11
def __init__(self, value):
self.y = value
self._c = 'private'
self.__d = 'more private'
z = 300
In this example:
- `x` is class variable,
- `_a` is private class variable (by naming convention),
- `__b` is private class variable (mangled by interpreter),
- `y` is instance variable,
- `_c` is private instance variable (by naming convention),
- `__d` is private instance variable (mangled by interpreter),
- `z` is local variable within scope of `__init__` method.
In case of single underscore in names, it's strictly a convention. It is still possible to access these variables. In case of double underscore names, they are mangled. It's still possible to circumvent that.
Problem
I come from Java, so I'm getting confused here. ``` class Sample(object): x = 100 # class var? def __init__(self, value): self.y = value # instance var? z = 300 # private var? how do we access this outside Sample? ``` What is the difference between the 3 variable declarations?