Creating a local variable scope.

Alf P. Steinbach alfps at start.no
Sun Nov 29 20:11:12 EST 2009


* markolopa:
> 
> On 18 Sep, 10:36, "markol... at gmail.com" <markol... at gmail.com> wrote:
>> On Sep 11, 7:36 pm, Johan Grönqvist <johan.gronqv... at gmail.com> wrote:
>>> I find several places in my code where I would like tohavea variable
>>> scope that is smaller than the enclosing function/class/module definition.
>> This is one of the single major frustrations I have with Python and an
>> important source of bugs for me. Here is a typical situation
> 
> Here is another bug that I just got. Twenty minutes lost to find it...
> 
> class ValueColumn(AbstractColumn):
>     def __init__(self, name, header, domain_names):
>         if type(domain_names) != tuple:
>             raise ValueError('a tuple of domain names must be given')
>         for name in domain_names:
>             if type(name) != str:
>                 raise ValueError('a tuple of domain names must be
> given')
>         self.domain_names = domain_names
>         super(ValueColumn, self).__init__(name, header)
> 
> The upper class was initialized with the wrong name, because the for
> loop to check
> domain_names used "name" which is also the argument to be passed.
> 
> If is an old thread but I am reopening to present real situation where
> this Python
> "feature" bothers me...

I think if one could somehow declare names as const (final, readonly, whatever) 
then that would cover the above plus much more.


Cheers,

- Alf



More information about the Python-list mailing list