Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #7234 > unrolled thread

Function declarations ?

Started byAndre Majorel <cheney@halliburton.com>
First post2011-06-08 13:58 +0000
Last post2011-06-13 17:19 -0700
Articles 10 — 8 participants

Back to article view | Back to comp.lang.python


Contents

  Function declarations ? Andre Majorel <cheney@halliburton.com> - 2011-06-08 13:58 +0000
    Re: Function declarations ? Calvin Spealman <ironfroggy@gmail.com> - 2011-06-08 10:06 -0400
    Re: Function declarations ? "D'Arcy J.M. Cain" <darcy@druid.net> - 2011-06-08 10:27 -0400
    Re: Function declarations ? Asen Bozhilov <asen.bozhilov@gmail.com> - 2011-06-10 15:46 -0700
      Re: Function declarations ? Andre Majorel <cheney@halliburton.com> - 2011-06-12 19:38 +0000
        Re: Function declarations ? Tim Roberts <timr@probo.com> - 2011-06-12 21:27 -0700
          Re: Function declarations ? Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2011-06-14 14:03 +1200
        Re: Function declarations ? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-06-13 14:58 +0000
          Re: Function declarations ? Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2011-06-14 13:18 +1200
        Re: Function declarations ? John Nagle <nagle@animats.com> - 2011-06-13 17:19 -0700

#7234 — Function declarations ?

FromAndre Majorel <cheney@halliburton.com>
Date2011-06-08 13:58 +0000
SubjectFunction declarations ?
Message-ID<slrniuv00f.8s9.cheney@atc5.vermine.org>
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.

[toc] | [next] | [standalone]


#7236

FromCalvin Spealman <ironfroggy@gmail.com>
Date2011-06-08 10:06 -0400
Message-ID<mailman.25.1307542038.11593.python-list@python.org>
In reply to#7234
Just write the function, at the top of the source. Easy peasy.

On Wed, Jun 8, 2011 at 9:58 AM, Andre Majorel <cheney@halliburton.com> 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

[toc] | [prev] | [next] | [standalone]


#7238

From"D'Arcy J.M. Cain" <darcy@druid.net>
Date2011-06-08 10:27 -0400
Message-ID<mailman.26.1307543256.11593.python-list@python.org>
In reply to#7234
On Wed, 8 Jun 2011 13:58:18 +0000 (UTC)
Andre Majorel <cheney@halliburton.com> 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 <darcy@druid.net>         |  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.

[toc] | [prev] | [next] | [standalone]


#7416

FromAsen Bozhilov <asen.bozhilov@gmail.com>
Date2011-06-10 15:46 -0700
Message-ID<e7913881-3028-4370-b4f5-7a7b17b04be0@h7g2000yqa.googlegroups.com>
In reply to#7234
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.

[toc] | [prev] | [next] | [standalone]


#7494

FromAndre Majorel <cheney@halliburton.com>
Date2011-06-12 19:38 +0000
Message-ID<slrniva5en.8s9.cheney@atc5.vermine.org>
In reply to#7416
On 2011-06-10, Asen Bozhilov <asen.bozhilov@gmail.com> 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.

[toc] | [prev] | [next] | [standalone]


#7501

FromTim Roberts <timr@probo.com>
Date2011-06-12 21:27 -0700
Message-ID<jv3bv65f8il7hl4k7gqbiom72e611j5kt8@4ax.com>
In reply to#7494
Andre Majorel <cheney@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@probo.com
Providenza & Boekelheide, Inc.

[toc] | [prev] | [next] | [standalone]


#7580

FromGregory Ewing <greg.ewing@canterbury.ac.nz>
Date2011-06-14 14:03 +1200
Message-ID<95ntriFod7U1@mid.individual.net>
In reply to#7501
Tim Roberts wrote:
> Andre Majorel <cheney@halliburton.com> 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

[toc] | [prev] | [next] | [standalone]


#7516

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2011-06-13 14:58 +0000
Message-ID<4df62578$0$30002$c3e8da3$5496439d@news.astraweb.com>
In reply to#7494
On Sun, 12 Jun 2011 19:38:42 +0000, Andre Majorel wrote:

> On 2011-06-10, Asen Bozhilov <asen.bozhilov@gmail.com> 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

[toc] | [prev] | [next] | [standalone]


#7578

FromGregory Ewing <greg.ewing@canterbury.ac.nz>
Date2011-06-14 13:18 +1200
Message-ID<95nr6tF92qU1@mid.individual.net>
In reply to#7516
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

[toc] | [prev] | [next] | [standalone]


#7575

FromJohn Nagle <nagle@animats.com>
Date2011-06-13 17:19 -0700
Message-ID<4df6a92e$0$2168$742ec2ed@news.sonic.net>
In reply to#7494
On 6/12/2011 12:38 PM, Andre Majorel wrote:
> On 2011-06-10, Asen Bozhilov<asen.bozhilov@gmail.com>  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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web