In data 14 giugno 2008 alle ore 08:55:58, Martin v. Löwis <[EMAIL PROTECTED]> 
ha scritto:

> This probably belongs to python-ideas or some such, but I don't
> think this approach can work. People will want to assign to local
> variables in an "ob" block, and then be surprised that the
> assignment actually modified their object:
>
> def f(L):
>     total = 0
>     for h in L:
>         on h:
>             more code accessing h's attributes
>             if x: # reads h.x
>                 total = total+1
>     return total
>
> People will be surprised that total is always 0 (and that
> some objects have an attribute total with a value of 1).
> Likewise
>
> on x:
>     for e in L:
>         counts[e] += 1 # modifies x.counts
>
> People will be surprised that x also grows an attribute
> e, as the for loop involves an assignment, which you say
> goes to the object's namespace, of course.
>
> Regards,
> Martin

I think there can be two solutions to the local variable assignament.

The first is to declare them with an "local" instruction, similar to the global 
one. So we can have:

def f(L):
    total = 0
    for h in L:
        on h:
            local total
            more code accessing h's attributes
            if x: # reads h.x
                total = total+1
    return total

on x:
    local e
    for e in L:
        counts[e] += 1 # modifies x.counts


The second solution is to leave assignament to the local namespace, and let 
assignament to object's attributes happens.
So your examples will work without changes, and the object never "inherits" new 
attributes.

Also, taking the Tk example that I used, it can be changed in the following way:

       on Button(self) as b:
           b.text = "QUIT"
           b.fg = "red"
           b.command = self.quit
 
           pack({"side": "left"})
 
       on Button(self) as b:
           b.text = "Hello"
           b.command = self.say_hi
 
           pack({"side": "left"})

Using a syntax which reseambles the with one.

Cesare Di Mauro
_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to