Function declarations ?

Discussion in 'Python' started by Andre Majorel, Jun 8, 2011.

  1. Is there a way to keep the definitions of the high-level
    functions at the top of the source ? I don't see a way to
    declare a function in Python.

    Thanks in advance.

    --
    André Majorel http://www.teaser.fr/~amajorel/
    J'ai des droits. Les autres ont des devoirs.
    Andre Majorel, Jun 8, 2011
    #1
    1. Advertising

  2. Just write the function, at the top of the source. Easy peasy.

    On Wed, Jun 8, 2011 at 9:58 AM, Andre Majorel <> wrote:
    > Is there a way to keep the definitions of the high-level
    > functions at the top of the source ? I don't see a way to
    > declare a function in Python.
    >
    > Thanks in advance.
    >
    > --
    > André Majorel http://www.teaser.fr/~amajorel/
    > J'ai des droits. Les autres ont des devoirs.
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >




    --
    Read my blog! I depend on your acceptance of my opinion! I am interesting!
    http://techblog.ironfroggy.com/
    Follow me if you're into that sort of thing: http://www.twitter.com/ironfroggy
    Calvin Spealman, Jun 8, 2011
    #2
    1. Advertising

  3. On Wed, 8 Jun 2011 13:58:18 +0000 (UTC)
    Andre Majorel <> wrote:
    > Is there a way to keep the definitions of the high-level
    > functions at the top of the source ? I don't see a way to
    > declare a function in Python.


    You don't declare functions in Python. You simply define them. You
    could define all your functions first and then call them. This is
    probably not a bad idea anyway. However, I suspect it doesn't solve
    your problem but since you haven't articulated it I can't tell.

    Don't present a solution and ask how it could work. Tell us what
    problem you are trying to solve and maybe we can suggest a solution.

    --
    D'Arcy J.M. Cain <> | Democracy is three wolves
    http://www.druid.net/darcy/ | and a sheep voting on
    +1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner.
    D'Arcy J.M. Cain, Jun 8, 2011
    #3
  4. Andre Majorel wrote:

    > Is there a way to keep the definitions of the high-level
    > functions at the top of the source ? I don't see a way to
    > declare a function in Python.


    I am not a Python developer, but Pythonic way of definition not
    declaration is definitely interesting. Languages with variable and
    function declarations usually use hoisted environment. JavaScript is
    the perfect example. Hoisted environment allows you to use call
    expression before the physical declaration of the function in the
    source text.

    e.g.

    foo();

    function foo() {}

    This code does not throw ReferenceError or TypeError in JavaScript. It
    calls `foo' exactly because it is used a hoisted environment. More
    precisely on entering in some context global or function JS engine
    define all function declarations as local scoped variables. While this
    has its advantages, it has a really big drawback. It cannot support
    default arguments which are bound to local variables.

    x = 10
    def f(y = x):
    print y

    f() #10

    This in hoisted environment cannot be implemented, because assignment
    is evaluating during the execution of the code, not on entering in
    specific context. So the interpreter is not able to predict the value
    for y here.

    Hope this helps, why Python use definitions instead of declarations.
    Asen Bozhilov, Jun 10, 2011
    #4
  5. On 2011-06-10, Asen Bozhilov <> wrote:
    > Andre Majorel wrote:
    >
    >> Is there a way to keep the definitions of the high-level
    >> functions at the top of the source ? I don't see a way to
    >> declare a function in Python.

    >
    > Languages with variable and function declarations usually use
    > hoisted environment.


    Hoisted ? With a pulley and a cable ?

    > JavaScript is the perfect example. Hoisted environment allows
    > you to use call expression before the physical declaration of
    > the function in the source text.


    The issue here is not the ability to call a function before its
    declaration. It's being able to do so before its definition.

    > Hope this helps, why Python use definitions instead of
    > declarations.


    It's not either/or. Any language has to provide a way to define
    functions whether or not it provides a way to declare them.

    Anyway, it seems the Python way to declare a function is

    def f ():
    pass

    Thanks everyone.

    --
    André Majorel http://www.teaser.fr/~amajorel/
    J'ai des droits. Les autres ont des devoirs.
    Andre Majorel, Jun 12, 2011
    #5
  6. On Sun, 12 Jun 2011 19:38:42 +0000, Andre Majorel wrote:

    > On 2011-06-10, Asen Bozhilov <> wrote:
    >> Andre Majorel wrote:
    >>
    >>> Is there a way to keep the definitions of the high-level functions at
    >>> the top of the source ? I don't see a way to declare a function in
    >>> Python.

    >>
    >> Languages with variable and function declarations usually use hoisted
    >> environment.

    >
    > Hoisted ? With a pulley and a cable ?


    No, with a compiler.

    "Hoisting" in computing refers to the idea of "lifting" variables or code
    outside of one block into another. For example, an optimization technique
    is to hoist repeated if statements outside the loop. E.g. instead of:

    for i in range(1000000):
    if condition:
    spam(i)
    else:
    ham(i)

    Assuming that condition doesn't change as the loop runs (spam and ham
    have no side-effects), this can be optimized to:

    if condition:
    for i in range(1000000):
    spam(i)
    else:
    for i in range(1000000):
    ham(i)

    That's called hoisting.

    In Python, which has first-class functions, we can do better by avoiding
    code duplication:

    if condition:
    func = spam
    else:
    func = ham
    for i in range(1000000):
    func(i)


    But either way, the decision is lifted (hoisted) out of the loop.



    >> JavaScript is the perfect example. Hoisted environment allows you to
    >> use call expression before the physical declaration of the function in
    >> the source text.

    >
    > The issue here is not the ability to call a function before its
    > declaration. It's being able to do so before its definition.


    Obviously you can't call a function before it is defined.

    As a language without type declarations, Python doesn't need functions to
    be declared ahead of time. I can define a function that calls another:

    >>> def ham(x):

    .... return 2*spam(x)
    ....
    >>>


    and so long as I eventually define spam() before calling ham(), all is
    good:

    >>> # much later

    .... def spam(x):
    .... return x-1
    ....
    >>> ham(3)

    4


    I can even re-define spam at runtime, and ham will see the difference:

    >>> def spam(x):

    .... return "spam "*x
    ....
    >>> ham(3)

    'spam spam spam spam spam spam '


    But what I can't do in Python is execute ham() BEFORE spam() is defined.
    (Well, I can, but I get an error.) Even if spam() is defined lower down
    in the source code, Python won't see it.

    As I understand it, that's not how Javascript works. The parser does two
    passes over the source file, instead of one like Python. If it sees a
    function definition, it hoists it out into the global environment, and
    then on the second pass executes the code as needed. Using Python syntax,
    this is an error:

    def ham(x):
    return 2*spam(x)

    print(ham(3))

    def spam(x):
    return x-1

    but Javascript will accept it perfectly fine, and output 4.


    Here's another model: Pascal. Because Pascal does type checking at
    compile time, it will reject the function ham() and raise a compiler
    error, because it can't test the argument and output type of spam(). So
    you would need a *forward declaration*. Using a mix of Python and Pascal
    syntax:


    # declare spam without a definition
    forward spam(x: integer): integer

    # declare and define ham
    def ham(x: integer): integer
    return 2*spam(x)

    print(ham(2))

    # define spam (declaration must match the earlier one!)
    def spam(x: integer): integer
    return x-1



    >> Hope this helps, why Python use definitions instead of declarations.

    >
    > It's not either/or. Any language has to provide a way to define
    > functions whether or not it provides a way to declare them.


    Exactly. Pascal has both; Python only has definitions.


    > Anyway, it seems the Python way to declare a function is
    >
    > def f ():
    > pass


    That doesn't declare a function. It defines a do-nothing function. `def`
    is an executable statement which happens at runtime, not a compile-time
    declaration.



    --
    Steven
    Steven D'Aprano, Jun 13, 2011
    #6
  7. Andre Majorel

    John Nagle Guest

    On 6/12/2011 12:38 PM, Andre Majorel wrote:
    > On 2011-06-10, Asen Bozhilov<> wrote:
    >> Andre Majorel wrote:
    >>
    >>> Is there a way to keep the definitions of the high-level
    >>> functions at the top of the source ? I don't see a way to
    >>> declare a function in Python.

    >>
    >> Languages with variable and function declarations usually use
    >> hoisted environment.

    >
    > Hoisted ? With a pulley and a cable ?


    There are languages with definitions and in which
    the compiler looks ahead. FORTRAN, for example.
    Python doesn't work that way. Nor do C and the
    languages derived from it, because the syntax
    is context-dependent. (In C++, "A b;" is ambiguous
    until after the declaration of A. In
    Pascal-derived languages, you write "var b: A;",
    which is parseable before you know what A is.
    So declarations don't have to be in dependency order.)

    None of this is relevant to Python, but that's
    what "hoisted" means in this context..

    John Nagle
    John Nagle, Jun 14, 2011
    #7
  8. Steven D'Aprano wrote:

    > "Hoisting" in computing refers to the idea of "lifting" variables or code
    > outside of one block into another.


    I'm not sure it's the right term for what's happening here,
    though. Nothing is being lifted to a higher level -- the
    functions remain at the same level they were written at.

    > Here's another model: Pascal. Because Pascal does type checking at
    > compile time, it will reject the function ham() and raise a compiler
    > error,


    It's more that Pascal is designed to make single-pass
    compilation easy. It's possible to support out-of-order
    declarations with compile-time type checking using two
    passes; Pyrex does this, for example.

    --
    Greg
    Gregory Ewing, Jun 14, 2011
    #8
  9. Tim Roberts wrote:
    > Andre Majorel <> wrote:
    >
    >>Anyway, it seems the Python way to declare a function is
    >>
    >> def f ():
    >> pass

    >
    > No, that DEFINES a function.


    Actually, it's more illuminating to say that it *creates* a function.

    The 'def' statement in Python is an executable statement. Executing
    it has the effect of creating a function object and binding it to
    the indicated name. Before that has happened, attempting to execute
    any code referring to that name will fail.

    Conversely, the function name doesn't need to be bound until the
    code referring to it is actually executed. So...

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


    works because by the time g is *called*, both def statements
    have been executed, and both function names have therefore been
    bound.

    --
    Greg
    Gregory Ewing, Jun 14, 2011
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave Theese

    Local function declarations

    Dave Theese, Sep 5, 2003, in forum: C++
    Replies:
    1
    Views:
    442
    Jack Klein
    Sep 5, 2003
  2. TechCrazy

    const in function declarations

    TechCrazy, Apr 21, 2005, in forum: C++
    Replies:
    6
    Views:
    375
    Grant Schoep
    Apr 24, 2005
  3. Marcus Lessard

    Mangled function type declarations?

    Marcus Lessard, Oct 17, 2003, in forum: C Programming
    Replies:
    1
    Views:
    338
    Eric Sosman
    Oct 17, 2003
  4. Billy Patton

    Function declarations/defaults

    Billy Patton, Nov 14, 2003, in forum: C Programming
    Replies:
    17
    Views:
    534
    Chris Torek
    Nov 19, 2003
  5. Douglas
    Replies:
    2
    Views:
    314
    Herbert Rosenau
    Jul 5, 2004
Loading...

Share This Page