Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > block scope?

Reply
Thread Tools

block scope?

 
 
John Nagle
Guest
Posts: n/a
 
      04-08-2007
Paul Rubin wrote:
> (Alex Martelli) writes:


> I have no opinion of this, locals() has always seemed like a crazy
> part of the language to me and I never use it. I'd be happy to see it
> gone since it makes compiling a lot easier.


I think of that, from a compiler perspective, as one of the features
that, if used, means you have to switch to a more inefficient representation.

I encourage the hard-code optimizing compiler people to keep plugging
away on Python. It's a convenient way to program, but the implementations
are slower than they should be a decade into the language's life cycle.

John Nagle
 
Reply With Quote
 
 
 
 
Alexander Schmolck
Guest
Posts: n/a
 
      04-08-2007
Neal Becker <> writes:

> One thing I sometimes miss, which is common in some other languages (c++),
> is idea of block scope. It would be useful to have variables that did not
> outlive their block, primarily to avoid name clashes. This also leads to
> more readable code.


I have on occassion used lambda as a poor-man's let, but only if I needed to
avoid multiple evaluation:

res = (lambda x=blah(...), y=blahz(...): f(x*y,x+y))()

I'm sure most people would debate it's more readable, but it's IMO superior to
cleaning up manually with ``del``. I sometimes also find it useful to avoid
cluttering up the interactive shell.

'as
 
Reply With Quote
 
 
 
 
Paddy
Guest
Posts: n/a
 
      04-08-2007
On Apr 7, 4:48 am, James Stroud <jstr...@mbi.ucla.edu> wrote:
> Neal Becker wrote:
> > One thing I sometimes miss, which is common in some other languages (c++),
> > is idea of block scope. It would be useful to have variables that did not
> > outlive their block, primarily to avoid name clashes. This also leads to
> > more readable code. I wonder if this has been discussed?

>
> Probably, with good code, block scope would be overkill, except that I
> would welcome list comprehensions to have a new scope:
>
> py> i
> ------------------------------------------------------------
> Traceback (most recent call last):
> File "<ipython console>", line 1, in <module>
> <type 'exceptions.NameError'>: name 'i' is not defined
>
> py> [i for i in xrange(4)]
> [0, 1, 2, 3]
> py> i # hoping for NameError
> 3


Yep, i think that we need consistent scope rules for
listexps and genexps. Isn't it coming in 3.0?

If it is, then maybe it will be back-ported to
Python 2.6.

In Python 2.5 we have the following:

>>> [k for k in (j for j in range(5))]

[0, 1, 2, 3, 4]
>>> k

4
>>> j

Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
NameError: name 'j' is not defined
>>>


- Paddy.

 
Reply With Quote
 
Georg Brandl
Guest
Posts: n/a
 
      04-08-2007
Alex Martelli schrieb:
> Paul Rubin <http://> wrote:
>
>> (Alex Martelli) writes:
>> > > exec?
>> > option 1: that just runs the compiler a bit later ...

>>
>> Besides exec, there's also locals(), i.e.
>> locals['x'] = 5
>> can shadow a variable. Any bad results are probably deserved

>
>>>> locals['x']=5

> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> TypeError: 'builtin_function_or_method' object does not support item
> assignment
>
> I suspect you want to index the results of calling locals(), rather than
> the builtin function itself. However:
>
>>>> def f():

> ... locals()['x'] = 5
> ... return x
> ...
>>>> f()

> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> File "<stdin>", line 3, in f
> NameError: global name 'x' is not defined
>
> No "shadowing", as you see: the compiler knows that x is NOT local,
> because it's not assigned to (the indexing of locals() does not count:
> the compiler's not expected to detect that), so it's going to look it up
> as a global variable (and not find it in this case).


Even assignments to real local variable names in the locals() result do
normally not result in the variable having a new value.

> I think that ideally there should be a runtime error when assigning an
> item of locals() with a key that's not a local variable name (possibly
> excepting functions containing exec, which are kind of screwy anyway).


I would make the locals() result completely independent from the frame,
and document that it is read only.

(though, this needs some other way for trace functions to interact with
the frame's local variables.)

Georg

 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      04-10-2007
Paul Rubin a écrit :
> (Alex Martelli) writes:
>
>>>>>locals['x']=5

>>
>>Traceback (most recent call last):
>> File "<stdin>", line 1, in <module>
>>TypeError: 'builtin_function_or_method' object does not support item
>>assignment

>
>
>
> Whoops, yeah, meant "locals()['x'] = 5".
>
>
>>I think that ideally there should be a runtime error when assigning an
>>item of locals() with a key that's not a local variable name (possibly
>>excepting functions containing exec, which are kind of screwy anyway).

>
>
> I have no opinion of this, locals() has always seemed like a crazy
> part of the language to me and I never use it. I'd be happy to see it
> gone since it makes compiling a lot easier.


I personally find locals() handy in few cases, like

def output():
foo = 42
bar = baaz()
quux = blah(foo, bar)
return "the %(bar)s is %(foo)d and the %(quux)s shines" % locals()

or:

class Foo(object):
@apply
def bar():
def fget(self):
return self._quux / 42
def fset(self, value):
self._quux = value * 42
return property(**locals())


I'd be very unhappy to see it gone...
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Fo:Block can you check to see if a block contains any text by using the block id? morrell XML 1 10-10-2006 07:18 PM
Problem with enterprise application block - data block Showjumper ASP .Net 1 03-19-2005 03:48 PM
Block DIV within a block DIV? Noozer HTML 3 01-06-2005 10:24 PM
XML schema validation of one xml block based on values from another xml block Andy XML 0 11-18-2004 11:04 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57