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


Groups > comp.lang.python > #102358

Re: carry **arguments through different scopes/functions

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Peter Otten <__peter__@web.de>
Newsgroups comp.lang.python
Subject Re: carry **arguments through different scopes/functions
Date Sun, 31 Jan 2016 14:49:56 +0100
Organization None
Lines 128
Message-ID <mailman.169.1454248208.2338.python-list@python.org> (permalink)
References <3ptWgs1F6Vz5vN2@dovecot03.posteo.de>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Trace news.uni-berlin.de 2fCKWrkfYkVxOYjSdzNWpQl29YHuYCszFzp3SzefK8RA==
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.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'resulting': 0.04; 'context': 0.05; 'python3': 0.05; 'sys': 0.05; '[1,': 0.09; 'mutable': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'simplified': 0.09; 'python': 0.10; 'python.': 0.11; 'question.': 0.13; 'def': 0.13; 'argument': 0.15; 'variables': 0.15; 'false:': 0.16; 'iterable': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'reproduce': 0.16; 'scopes': 0.16; 'true:': 0.16; 'variable.': 0.16; 'wrote:': 0.16; 'attribute': 0.18; 'skip:` 10': 0.18; 'variable': 0.18; 'changes': 0.20; 'own.': 0.22; 'import': 0.24; 'header:User-Agent:1': 0.26; 'example': 0.26; 'header:X-Complaints-To:1': 0.26; 'skip:# 10': 0.27; 'skip:t 40': 0.27; 'least': 0.27; 'back.': 0.27; 'actual': 0.28; 'subject:/': 0.30; 'that.': 0.30; 'too.': 0.30; 'code': 0.30; 'skip:[ 10': 0.31; 'probably': 0.31; 'certain': 0.31; 'skip:_ 10': 0.32; 'skip:. 10': 0.32; 'topic': 0.32; 'problem': 0.33; 'could': 0.35; 'false': 0.35; 'item': 0.35; 'but': 0.36; 'should': 0.36; 'there': 0.36; 'skip:{ 10': 0.36; 'structures': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'turn': 0.37; 'received:org': 0.37; 'things': 0.38; 'data': 0.39; 'sure': 0.39; 'does': 0.39; "didn't": 0.39; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'some': 0.40; 'your': 0.60; 'bring': 0.62; 'course': 0.62; 'more': 0.63; 'goal': 0.64; 'here': 0.66; 'offer': 0.66; 'subject': 0.70; 'subject.': 0.72; 'special': 0.73; 'idiomatic': 0.84; 'toy': 0.84
X-Injected-Via-Gmane http://gmane.org/
X-Gmane-NNTP-Posting-Host p57bd88cd.dip0.t-ipconnect.de
User-Agent KNode/4.13.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
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>
Xref csiph.com comp.lang.python:102358

Show key headers only | View raw


c.buhtz@posteo.jp wrote:

> I am not sure what the problem is here, so I don't really know how I
> should call the subject for that question. Please offer a better
> subject.
> 
> The code below is a extrem simplified example of the original one. But
> it reproduce the problem very nice. Please focus on the variable
> `return_code`.
> 
> There is a `list()` of numbers without the number `7` in. The code
> check if the number `7` is in and should tell that it is not in. But it
> does tell me that `7 is in`. ;)
> 
> I think I didn't know some special things about scopes of variables in
> Python. This might be a very good problem to learn more about that. But
> I don't know on which Python topic I should focus here to find a
> solution for my own.
> 
>     #!/usr/bin/env python3
>     import sys
>     
>     def walkOn_ids(ids, handlerFunction, **handlerArgs):
>         for one_id in ids:
>             handlerFunction(one_id=one_id, **handlerArgs)
>             print('after handler-call for id {}\t{}'
>                   .format(one_id, handlerArgs))
>     
>     
>     def _on_id(one_id, return_code):
>         if return_code is False:
>             return
>     
>         if one_id == 7:
>             return_code = True
>         else:
>             return_code = False
>     
>         print('one_id: {}\treturn_code: {}'.format(one_id, return_code))
>     
>     
>     def _isSevenInIt(ids):
>         return_code = True
>     
>         walkOn_ids(ids=ids,
>                    handlerFunction=_on_id,
>                    return_code=return_code)
>     
>         return return_code
>     
>     
>     ids = [1,2,3,4,5,6,8,9]  # NO 7
>     print(ids)
>     
>     if _isSevenInIt(ids) is True:
>         print('7 is in')
>     else:
>         print('no 7 in it')
>     
>     sys.exit()
> 
> Of course I could make `return_code` a `global` variable. But that is
> not the goal. The goal is to carry this variable inside the
> walker-function and bring the result back. In the original code I will
> use some more complexe data structures with `**handlerArgs`.

The solution that requires the least changes to your code is probably to 
turn return_code into an attribute of a mutable argument (called context 
below):

#!/usr/bin/env python3
import sys
import types


def walkOn_ids(ids, handlerFunction, **handlerArgs):
    for one_id in ids:
        handlerFunction(one_id=one_id, **handlerArgs)
        print('after handler-call for id {}\t{}'
              .format(one_id, handlerArgs))


def _on_id(one_id, context):
    if context.return_code:
        return

    if one_id == 7:
        context.return_code = True

    print('one_id: {}\treturn_code: {}'.format(one_id, context.return_code))


def _isSevenInIt(ids):
    context = types.SimpleNamespace(return_code=False)

    walkOn_ids(ids=ids,
               handlerFunction=_on_id,
               context=context)

    return context.return_code


ids = [1, 2, 3, 4, 5, 6, 8, 9]  # NO 7
print(ids)

if _isSevenInIt(ids):
    print('7 is in')
else:
    print('no 7 in it')

However, the resulting code is not very pythonic.

In idiomatic Python the toy problem you present -- looking for the first 
item in an iterable that fulfills a certain condition -- would be solved 
like that:

def equals_seven(id):
    return id == 7

ids = [1, 2, 3, 4, 5, 6, 8, 9]  # NO 7

if any(equals_seven(id) for id in ids):
    print('7 is in')
else:
    print('no 7 in it')

It is likely that your actual code can be simplified, too.

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


Thread

Re: carry **arguments through different scopes/functions Peter Otten <__peter__@web.de> - 2016-01-31 14:49 +0100

csiph-web