init inside def

bruno.desthuilliers at gmail.com bruno.desthuilliers at gmail.com
Mon Oct 25 06:39:55 EDT 2010


On 25 oct, 12:05, targetsmart <targetsm... at gmail.com> wrote:
> Hi,
> today I just came across a python code snippet where __init__ was
> defined inside a function..
> I am not able to understand the reason why
>
> The code snippet was similar like
>
> def func1(a,b):
>   def __init__():
>     func2(a,b)
>   def func2(a,b):
>     if a == b:
>       return True
>     else:
>       return False
>   return False
>
> So far I have seen __init__ only used inside class definitions not
> inside any function, could somebody tell me how __init__ can be useful
> inside a function definition..?

__init__ as an inner function name has no special meaning. And in the
above snippet it happens to to be totally useless since it's not
neither called nor returned nor used in any way. In fact, since func2
is not used neither (and totally braindead FWIW), the above snippet is
functionally equivalent to:

def func1(a, b):
    return False

Perhaps the real snippet would make more sense ?





More information about the Python-list mailing list