Re: [Python-ideas] Quick idea: defining variables from functions that take the variable name

I agree that cleaning up is not really needed. I think the problem is that people feel bad about having to type things twice and immediately think "It's violating DRY". And some are quite adamant about not violating DRY. Fact of the matter: on nearly all relevant scales, this "DRY violation" does not matter, in fact, I would not even want to call it a violation. DRY is about having 1 representative location for knowledge. The name of a variable (1) hardly counts as knowledge and (2) is also used at other locations (use-sites). Is repeating the variable name that much of a deal? Granted, I mostly only use namedtuple of all the given examples. And I use that sparingly. Are `TypeVar` and `Symbol` used more often? At least for namedtuple, we could do some meta-classy logic to allow for class Point(namedtuple): _fields = ('x', 'y') And for namedtuple it makes sense. For TypeVar it might too? class T(TypeVar): pass Symbol? Not so much, I think. I'm not eager to see "improvements" in this area, as I do not see it as a problem, except for those with an overly strict view on DRY. Any new syntax/semantics for this would need to bring a real obvious improvement for the general case, and I doubt that will happen. The one suggestion I have heard that remotely makes sense (and it was not even in this thread!) is the concept of decorators for variables @Something x = 5 would become x = Something('x', 5) And even that idea is bad: what if you want multiple arguments? Would you need a tuple? What if you don't need any arguments? (e.g. Symbol). After seeing all the suggestions so far, I'm not convinced by any of them that they are an improvement. So unless there's a suggestion for syntax/semantics that is reasonable to become the only **obvious** way to do it, I'd say nay.

Sjoerd Job Postmus wrote:
I don't know whether it meets the strict definition of DRY or not, but the reason it's annoying is that it's a repitition that doesn't convey any information. Using a variable name in multiple places tells you something: "this thing here refers to that thing defined over there". But having the variable name in two places in the *definition* tells you nothing. -- Greg

Sjoerd Job Postmus wrote:
I don't know whether it meets the strict definition of DRY or not, but the reason it's annoying is that it's a repitition that doesn't convey any information. Using a variable name in multiple places tells you something: "this thing here refers to that thing defined over there". But having the variable name in two places in the *definition* tells you nothing. -- Greg
participants (2)
-
Greg Ewing
-
Sjoerd Job Postmus