> did I miss something?
In your examples you're binding or rebinding to vars in the current scope. The issue is rebinding vars outside of the current scope.Perhaps this code demonstates what I'm getting at.
def my_function():
a = 1
def modify_a():
a = 2
modify_a()
print a # prints 1
#
b = [1]
def modify_b():
b = 2
modify_b()
print b[0] # prints 1
#
c = [1]
def modify_c():
c[0] = 2
modify_c()
print c[0] # prints 2
my_function()This is clearer, but it has absolutely nothing to do with `for` loops (which is why your statement utterly confused me) and everything to do with Python's broken scoping in general: Python 2 gives no writing access to lexical scopes between global and local (Python 3 added a new keyword, `nonlocal`, which is used in the same manner as `global` to set a binding as writable in a parent scope).
You can create classes inline via `type`, and Python 2.6 has named tuples which are basically record types. You could also create a one-off class onto which you'd graft attributes as needed:
you can even use `copy.copy` (or `copy.deepcopy`) on these to have something prototypey: > You could also use a dictionary, but I think that's a bit more verbose.By about 4 characters per key access.
> In python there is ugliness involved in tapping a new function onto an existing object. […] When I encounter this in python I usually do a lambda trick to make the function a member of the object
What lambda trick? Why not just create a (bound) instance method from your function and graft that onto your object? Granted it's not going to be as sexy as doing so in IO, but it takes all of one line:
> I have and will never use multiple inheritance in python and would prefer the simple syntax that would be made possible with single inheritance.If you use single inheritance always, you can just get rid of `super` altogether: just call the relevant method on your superclass.
> [1] In python you have to use a structure for this, because once you're in the 'for' loop, it won't let you rebind variables outside the scope of the for loop, only modify them.
I hope I misunderstand what you mean, because what I'm reading here is downright wrong: Python only has function-level scoping (and a special scope for listcomps, but that's a different issue) so any binding you create or modify within a for loop (or a `with` statement, or an `if` or whatever else you may think of) will be visible from outside the block:
And of course, you can rebind existing variables just fine: did I miss something?