Comment by vbezhenar 2 months ago So Python is not OOP language? You can't hide fields. 6 comments vbezhenar Reply nr378 2 months ago You can hide fields in Python with a little bit of gymnastics: class EncapsulatedCounter: def __init__(self, initial_value): _count = initial_value def increment(): nonlocal _count _count += 1 return _count self.increment = increment counter = EncapsulatedCounter(100) new_value = counter.increment() print(f"New value is: {new_value}") d0mine 2 months ago Usually, a simple function is enough: def make_counter(start=0): count = start def incr(): nonlocal count count += 1 return count return incr Example: >>> c = make_counter() >>> c() 1 >>> c() 2 But it hides nothing: >>> c.__closure__[0].cell_contents 2 >>> c.__closure__[0].cell_contents = -1 >>> c() 0 "private" in Python is cultural, not enforced. (you can access `self.__private` from outside too if you want). bluGill 2 months ago It has conventions to hide data. Good enough. vips7L 2 months ago Python was a mistake if you ask me ¯\_(ツ)_/¯ cpburns2009 2 months ago Python is the worst programming language except for all of the others. vips7L 2 months ago I don’t know man. It’s honestly the last thing I would choose. There is literally always something else that is more appetizing.
nr378 2 months ago You can hide fields in Python with a little bit of gymnastics: class EncapsulatedCounter: def __init__(self, initial_value): _count = initial_value def increment(): nonlocal _count _count += 1 return _count self.increment = increment counter = EncapsulatedCounter(100) new_value = counter.increment() print(f"New value is: {new_value}") d0mine 2 months ago Usually, a simple function is enough: def make_counter(start=0): count = start def incr(): nonlocal count count += 1 return count return incr Example: >>> c = make_counter() >>> c() 1 >>> c() 2 But it hides nothing: >>> c.__closure__[0].cell_contents 2 >>> c.__closure__[0].cell_contents = -1 >>> c() 0 "private" in Python is cultural, not enforced. (you can access `self.__private` from outside too if you want).
d0mine 2 months ago Usually, a simple function is enough: def make_counter(start=0): count = start def incr(): nonlocal count count += 1 return count return incr Example: >>> c = make_counter() >>> c() 1 >>> c() 2 But it hides nothing: >>> c.__closure__[0].cell_contents 2 >>> c.__closure__[0].cell_contents = -1 >>> c() 0 "private" in Python is cultural, not enforced. (you can access `self.__private` from outside too if you want).
vips7L 2 months ago Python was a mistake if you ask me ¯\_(ツ)_/¯ cpburns2009 2 months ago Python is the worst programming language except for all of the others. vips7L 2 months ago I don’t know man. It’s honestly the last thing I would choose. There is literally always something else that is more appetizing.
cpburns2009 2 months ago Python is the worst programming language except for all of the others. vips7L 2 months ago I don’t know man. It’s honestly the last thing I would choose. There is literally always something else that is more appetizing.
vips7L 2 months ago I don’t know man. It’s honestly the last thing I would choose. There is literally always something else that is more appetizing.
You can hide fields in Python with a little bit of gymnastics:
Usually, a simple function is enough:
Example:
But it hides nothing:
"private" in Python is cultural, not enforced. (you can access `self.__private` from outside too if you want).
It has conventions to hide data. Good enough.
Python was a mistake if you ask me ¯\_(ツ)_/¯
Python is the worst programming language except for all of the others.
I don’t know man. It’s honestly the last thing I would choose. There is literally always something else that is more appetizing.