5 ms·
Or it's a named stateful or immutable data container with validation? You don't always want to use primitive types. But I am not a Python dev.
by TomMarius 6y ago
Or it's a named stateful or immutable data container with validation? You don't always want to use primitive types. But I am not a Python dev.
- moonchild 6y agoPython doesn't have private members, so that doesn't help you as much.
- heavenlyblue 6y agoPython has private members through double underscore methods
- ben-schaaf 6y agoThey're only private in a sense that you're not supposed to access them, there's no enforcement by the language in any way beyond renaming them `_<class>__<attr>`. They're still accessible and they still show up in `dir`, `__dict__`, etc.
- travisjungroth 6y agoThat’s not their purpose and they’re not really private. Leading double underscore methods cause name mangling (the class name gets added to the method name) and they’re used for solving specific problems with inheritance. Don’t use them just to make something private. You’re screwing up your inheritance if you don’t realize what you’re doing. Leading single underscore is private by convention. Still should generally be avoided, but it’s the proper use.
- cessor 6y agoMembers in Python are never private. Variables that are supposed to be used internally only are marked with and underscore, but that just conveys intent and isn't enforced by the interpreter/runtime. But you can emulate private data like this: >>> def a(u, v): ... def b(): ... return u + v ... return b ... >>> b = a(1,2) >>> b() 3 Like this, there is no way to access u & v from b.
- moonchild 6y agoLooks like classes can't be closures, though: >>> def f(): ... x = 5 ... class Blub: ... def incx(self): ... x += 1 ... def getx(self): ... return x ... return Blub() ... >>> j = f() >>> j.incx() Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 5, in incx UnboundLocalError: local variable 'x' referenced before assignment
- cessor 6y agoWhen using it like this, the interpreter opens a new namespace where the variables are bound. The x is declared in the outer scope, so it can't find it. However, you can ask python to keep searching vor the name `x` in the closest outer scope, that is what the `nonlocal` statement is for: def f(): x = 5 class Blub: def incx(self): nonlocal x x += 1 def getx(self): return x return Blub() j = f() j.incx() print(j.getx()) This will print 6, as expected.
- sideshowb 6y agoOr anything that needs validation before you try to run the main part. e.g. the init() method may give you a shopping list of data you're going to need to pass to run() but which couldn't have been deduced until init() validated its parameters. Sure, you could handle this the functional way by passing a getData() function to run() so it can do so itself but I'm not sure that's any more readable.
- makapuf 6y agoThen build a function from validated parameters (or partially apply some parameters).
- deleted 6y ago[deleted]
- j88439h84 6y agoYes, this is the point of `dataclasses` and `attrs`.