Function declarations ?

Tim Roberts timr at probo.com
Mon Jun 13 00:27:31 EDT 2011


Andre Majorel <cheney at halliburton.com> wrote:
>
>Anyway, it seems the Python way to declare a function is
>
>  def f ():
>    pass

No, that DEFINES a function.  There is no way to declare a function in
Python.  It isn't done, because it isn't necessary.

That code doesn't do what you think it does.  Example:

    def f():
        pass

    g = f

    def f():
        return 3

    print g()

That prints "none".  That module has two definitions of f, not one.  The
meaning of the name "f" changes partway through the module.

Python is not C.  You need to use Python habits, not C habits.  What
construct led you to think you need to declare a function like that?  This
code, for example, works fine:

    def g():
        return f()
    def f():
        return 3
    print g()

The name "f" does not have to be defined until the function "g" is actually
executed.
-- 
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.



More information about the Python-list mailing list