Been reading up on #Python lately. I know that classes in Python "doesn't support" private values, but could this functionality not be implemented with closures?
right indeed count can't be directly accessed here, and it's a bit more difficult to bypass, but it's still possible.
edit: though even in java or c++, you can do horrible stuff to access stuff that were declared "private", it's also a bad idea, but since people use libraries that sometime made stupid decisions, they will sometimes do it, because they can't find another way.
something like this? Sure the class can save the value in a method and return that later, but then i get the same problem, anything can decide to patch this method instead.
well, it's not very clean, as self modifying code is not a good friend of code that is easy to reason about and maintain, and that will likely confuse type checkers and other tools. And it doesn't solve the problem, since i can totally do cl.get_val = lambda: "test" instead of calling cl.set_val("test").
@Gabriel Pettier That's true. It's not what I would consider to be a *good* idea. I was just reading in the docs where it said Python had *no way* of creating private variables, and I thought: Is that *really* true?
At the very least, it's fair to say that there is no official way.
Someone argued that Python is a language for adults and "private" variables are therefore not needed. There will always be ways to access them anyway but responsible programmers would just leave them alone.
If the #Python rules and conventions of _ prefixes don't meet the needs for 'privacy', perhaps choose another tool from the tool-box instead of trying to hammer-in a screw?
Jonathan Lamothe
Content warning: boring nerd stuff (example code)
Wouldn't this effectively make
count
into a private variable (granted, one that *couldn't* be inherited)?There's probably a better way of writing it too.
Gabriel Pettier
•Content warning: boring nerd stuff (example code)
edit: though even in java or c++, you can do horrible stuff to access stuff that were declared "private", it's also a bad idea, but since people use libraries that sometime made stupid decisions, they will sometimes do it, because they can't find another way.
Gabriel Pettier
•Jonathan Lamothe
Gabriel Pettier
•Jonathan Lamothe
At the very least, it's fair to say that there is no official way.
Maurice LéDuck
•Jonathan Lamothe
dn
•Jonathan Lamothe
That said, it was more of a hypothetical question.