It seems like most "code camp" teachers know just enough to give a passable performance. There's no depth of knowledge but given the brevity of these courses, maybe it's a moot point. Still, this guy should focus less on his Brand and more on getting up to snuff.
We may laugh and ignore this guy, but these kind of posts can seriously hurt and confuse beginners.
What sucks is that this guy seems genuinely nice and wants to help... but IDK. I personally would never post anything unless I am 100% sure it's correct (when applicable).
"This is the main difference between public and private variables. We can’t direct access or set a new value out of our class."
He should just start off by saying that there are no private methods or variables, but instead a naming convention should be used to discourage users of the class from accessing them directly.
>>> class A:
... def __a(self,b):
... return b
...
>>> A()
<__main__.A object at 0x0000024EE5B0EBE0>
>>> A().__a('b')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'A' object has no attribute '__a'
>>> a=A()
>>> dir(a)
['_A__a', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__',
'__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__',
'__sizeof__', '__str__', '__subclasshook__', '__weakref__']
>>> a._A__a("b")
'b'
In practice, I have never seen this used and just confuses things - and for dev's coming from Java / C++ or some other language it tends to just confuse things.Python has an idiomatic way to control attribute access - descriptors.
Mashing Java getter/setter methods into Python is so, so unpythonic.
[0] https://docs.python.org/2/tutorial/classes.html#private-vari...
That's not. The use case for name mangling was "avoid the risk of conflict when designing classes for inheritance": if you're building classes for inheritance, subclasses using the same name for an internal/private attribute are not going to unwittingly collide with the base classe's.
A single underscore is something that's not part of the classes public API, any child classes may need to change attributes, and by using a double underscore you are saying "I know better than you, you won't ever need to change this", which is never true.
Python isn't like Java where you often have complex and often convoluted class heirachies, where truly private attributes might be more useful.
A single leading underscore is a convention to mark a variable as private.
A double leading underscore "name mangles" the variable (adds the class to the name of the variable) and has a specific use case for inheritance. They're class level private, which is probably not what you want.
class Number:
def __init__(self, n):
self._number = n
self.__number = n
class NumberChild(Number):
def one_higher(self):
return self._number + 1
def two_higher(self):
return self.__number + 2
number_child = NumberChild(10)
number_child.one_higher() # This works
number_child.two_higher() # This raises an Attribute Error