
Calvin Spealman wrote:
On Sat, Jun 30, 2012 at 9:59 AM, <fiatjaf@yahoo.com.br> wrote:
the idea is to make an variable assignment at the same time that the existence of that variable -- which is being returned by a function -- is made.
suppose we are returning a variable from the method 'get' from the 'request' object and them making some stuff with it, but that stuff we will only do if it exists, if not, we'll just pass, instead of writing:
variable = self.request.get('variable') if variable: print variable
we could write
if self.request.get('variable') as variable: print variable
seems stupid (or not?), but with lots of variables to process, this pre-assignment could be very unpleasant -- especially if, as the in the example case, very little use will be made of the tested variable.
also, the "as" expression already exists and is very pythonic.
This is probably the best solution to the problem
What problem? I'm not convinced that what fiatjaf@yahoo.com.br describes is an actual problem that needs solving. So what if you have to bind a reference to a name before using it in an if statement? That's not a problem. "Solving" it doesn't make programming any easier, or give you more power. There is absolutely no difference in programming power or expressiveness between: if func(a, b) as x: process(x) and x = func(a, b) if x: process(x) (In fact, I would argue that the second is *slightly* more readable, as the name binding comes first, not last.) At best, this proposal merely adds a trivial bit of syntactic sugar to the language. Contrast that with "import as", which truly does add expressiveness to the language. Working around the lack of "import as" if Python didn't have it would not be so easy. The obvious solution is also the wrong solution: # import long_name as ln import long_name ln = long_name del long_name That's wrong, because it has the side-effect of replacing, then deleting, any existing binding to long_name. "import as" does not do that. Good suggestions for syntactic sugar should add power or expressiveness to the language, not merely save a line of code or a few keystrokes. As given, this idea becomes one more special case for people to learn, with no corresponding benefit. -1 on the idea as given. By the way, to the Original Poster, please configure your mail client to display a name that we can refer to you by, or sign your posts. It doesn't have to be your real name, just something that you would like to be known as. It is annoying to have to refer to you by your email address. -- Steven