[Python-ideas] Decorators for variables
boekewurm at gmail.com
Sun Apr 3 02:05:36 EDT 2016
> But that doesn't change the behaviour of the *name* "a", it
> only sets the value it is bound to. Nothing stops anyone from saying:
> a = 42
> and re-binding the name to another value which is no longer a Char.
> Python has no default support for running custom code when binding
> arbitrary names to a value. To get this sort of thing to work, you are
> limited to attributes, using the descriptor protocol.
This is exactly my point. When they implemented descriptors (2.2) they did
that to add to the new-style class system. I think they did a great job,
but namespaces were overlooked in my opinion. Why should we be limited to
class attributes when using descriptors? Any namespace which contains a
'.__dict__' content mapping should be able to hold descriptors in my
opinion. If this has been discussed before, then please link me to the
relevant discussion, I'd love to read the points made.
> But this isn't an advantage in the case of the variable
Assigning variables from function results is fairly common. When chaining
function calls to get the desired behaviour of the variable, it will get
confusing which part is the 'value' part, and which part is the 'behaviour'
part, apart from namings:
var = logging(
would get more clear if you wrote it this way:
var = factorize(4)
Something I just thought about: currently you can use the property() call
to make an attribute have descriptor properties. This may be somewhat
controversial, but maybe limit this to decorators only? While keeping the
'.__dict__' override method open, the way things work currently won't
change that much, but it will make the assignment of attributes or
variables with descriptor properties a lot more intuitive, as you do not
set a *name* to a variable and then can undo that just a while later:
var = property(setter, getter, deleter, docs)
var = 20
currently changes behaviour depending on what kind of scope it is located
in (class description, any other scope), while decorators (for functions at
least) work in every scope I can think of. I think that is strange, and
that it should just be the same everywhere. Using decorators here could be
a very nice and interesting choice.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-ideas