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


Groups > comp.lang.python > #79772

Re: Callback functions arguments

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'argument': 0.05; 'tkinter': 0.07; 'arguments': 0.09; 'callback': 0.09; 'parameter': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; 'def': 0.12; "'b'": 0.16; 'argument,': 0.16; 'argument:': 0.16; 'ast': 0.16; 'b):': 0.16; 'expects': 0.16; 'positional': 0.16; 'raised.': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'typeerror:': 0.16; 'wrote:': 0.18; 'widget': 0.19; '>>>': 0.22; 'example': 0.22; 'header:User-Agent:1': 0.23; 'passes': 0.24; 'pass': 0.26; 'code:': 0.26; 'header:X-Complaints-To:1': 0.27; 'function': 0.29; 'scale': 0.29; "doesn't": 0.30; "skip:' 10": 0.31; '"",': 0.31; 'piece': 0.31; 'file': 0.32; 'probably': 0.32; 'supposed': 0.32; 'url:python': 0.33; '(most': 0.33; 'guess': 0.33; 'knows': 0.35; 'but': 0.35; 'module.': 0.36; 'error.': 0.37; 'clear': 0.37; 'to:addr:python-list': 0.38; 'recent': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'how': 0.40; 'new': 0.61; "you'll": 0.62; 'different': 0.65; 'here': 0.66; 'you:': 0.81; 'url:free': 0.84; 'url:php': 0.85; 'url:fr': 0.93
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: Callback functions arguments
Date Mon, 27 Oct 2014 09:52:33 +0100
Organization None
References <544dff5d$0$2073$426a74cc@news.free.fr>
Mime-Version 1.0
Content-Type text/plain; charset="UTF-8"
Content-Transfer-Encoding 8Bit
X-Gmane-NNTP-Posting-Host p57bd88e3.dip0.t-ipconnect.de
User-Agent KNode/4.13.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.15231.1414399974.18130.python-list@python.org> (permalink)
Lines 71
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1414399974 news.xs4all.nl 2872 [2001:888:2000:d::a6]:52385
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:79772

Show key headers only | View raw


ast wrote:

> Hi
> 
> In this web site at example n°5
> http://fsincere.free.fr/isn/python/cours_python_tkinter.php
> 
> A program is using the "Scale" widget from tkinter module.
> Here is a piece of code:
> 
> Valeur = StringVar()
> 
> echelle = Scale(Mafenetre, from_=-100, to=100, resolution=10, \
> orient=HORIZONTAL, length=300, width=20, label="Offset", \
> tickinterval=20, variable=Valeur, command=maj)
> 
> The "maj" callback function is:
> 
> def maj(nouvelleValeur):
>     print(nouvelleValeur)
> 
> When the user move the scale with the mouse, the new position
> is supposed to be printed on the python shell.
> 
> The "maj" function has an argument "nouvelleValeur" but no
> argument is passed through the Scale widget.
> 
> So how the hell Python knows that it has to pass parameter
> "Valeur" to the "maj" function ?

Python doesn't "know" it has to pass an argument, it just does it. Change 
the callback to

def maj():
    print("no args")

and you'll get an error. If I were to guess

> echelle = Scale(Mafenetre, from_=-100, to=100, resolution=10, \
> orient=HORIZONTAL, length=300, width=20, label="Offset", \
> tickinterval=20, variable=Valeur, command=maj)

you probably are misled by the 'command=maj' part in the above line. This 
means that the function is passed and is different from command=maj() where 
the *result* of the function is passed. 

Here's a self-contained example that may clear things up for you:

>>> def call_them(one, two):
...     one(1)
...     two(2, 3)
... 
>>> def square(a):
...     print(a, "*", a, "=", a*a)
... 
>>> def product(a, b):
...     print(a, "*", b, "=", a*b)
... 
>>> call_them(square, product)
1 * 1 = 1
2 * 3 = 6
>>> call_them(product, product)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in call_them
TypeError: product() missing 1 required positional argument: 'b'

call_them() expects that one() takes 1 argument and two() takes 2 arguments. 
If the user passes a function that expects a different number of arguments a 
TypeError is raised.

Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Callback functions arguments "ast" <nomail@invalid.com> - 2014-10-27 09:16 +0100
  Re: Callback functions arguments Peter Otten <__peter__@web.de> - 2014-10-27 09:52 +0100
    Re: Callback functions arguments "ast" <nomail@invalid.com> - 2014-10-28 08:35 +0100
      Re: Callback functions arguments Chris Angelico <rosuav@gmail.com> - 2014-10-28 18:51 +1100
        Re: Callback functions arguments "ast" <nomail@invalid.com> - 2014-10-28 12:14 +0100

csiph-web