Chris Angelico <ros...@gmail.com>:
> On Sun, Aug 19, 2018 at 9:03 AM, Marko Rauhamaa <ma...@pacujo.net> wrote:
>> Chris Angelico <ros...@gmail.com>:
>>
>>> *headscratch*
>>>
>>> So this is okay:
>>>
>>> def f():
>>>     for i in range(5):
>>>         def g(): ...
>>>
>>> But this isn't:
>>>
>>> class C:
>>>     for i in range(5):
>>>         def m(self): ...
>>>
>>> I've missed something here.
>>
>> No, you got it right.
>
> Then I've completely missed the problem. Why is one of them acceptable
> and the other not?

In the def-def case, you will do something mundane with g. For example,
you will register it as a callback.

In the class-def case, you are defining the method m five times in the
same namespace and overwriting all but one of the definitions, which
probably isn't what you are after.

In order to populate the class with methods of different names, you will
need to manipulate the namespace programmatically. If you find yourself
needing to do something like that, you need to take a couple of steps
back and ask yourself if there might be a more conventional way to solve
the problem at hand.


Marko
-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to