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


Groups > comp.lang.python > #102350

carry **arguments through different scopes/functions

Path csiph.com!feeder.erje.net!1.eu.feeder.erje.net!fu-berlin.de!uni-berlin.de!not-for-mail
From <c.buhtz@posteo.jp>
Newsgroups comp.lang.python
Subject carry **arguments through different scopes/functions
Date Sun, 31 Jan 2016 13:19:58 +0100
Lines 63
Message-ID <mailman.162.1454243016.2338.python-list@python.org> (permalink)
Mime-Version 1.0
Content-Type text/plain; charset=US-ASCII
Content-Transfer-Encoding 7bit
X-Trace news.uni-berlin.de 4BNTqDgtqG7TbeUOjzlZkw4gAJDGOtp3AzaewUNErdFA==
Return-Path <c.buhtz@posteo.jp>
X-Original-To Python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.008
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'else:': 0.03; 'python3': 0.05; 'sys': 0.05; 'received:172.16.0': 0.09; 'simplified': 0.09; 'python': 0.10; 'python.': 0.11; 'question.': 0.13; 'def': 0.13; 'variables': 0.15; 'false:': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'reproduce': 0.16; 'scopes': 0.16; 'true:': 0.16; 'variable.': 0.16; 'skip:` 10': 0.18; 'variable': 0.18; 'own.': 0.22; 'import': 0.24; 'example': 0.26; 'skip:# 10': 0.27; 'back.': 0.27; 'subject:/': 0.30; 'that.': 0.30; 'code': 0.30; 'skip:[ 10': 0.31; 'skip:_ 10': 0.32; 'skip:. 10': 0.32; 'topic': 0.32; 'problem': 0.33; 'could': 0.35; 'false': 0.35; 'but': 0.36; 'should': 0.36; 'there': 0.36; 'skip:{ 10': 0.36; 'structures': 0.36; 'to:addr:python-list': 0.36; 'really': 0.37; 'charset:us-ascii': 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; 'from:no real name:2**0': 0.60; 'header:Message-Id:1': 0.61; '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; 'received:185': 0.91
X-Mailer Claws Mail 3.9.3 (GTK+ 2.24.23; i686-pc-linux-gnu)
X-Mailman-Approved-At Sun, 31 Jan 2016 07:23:35 -0500
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:102350

Show key headers only | View raw


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`.

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


Thread

carry **arguments through different scopes/functions <c.buhtz@posteo.jp> - 2016-01-31 13:19 +0100
  Re: carry **arguments through different scopes/functions Steven D'Aprano <steve@pearwood.info> - 2016-02-01 12:53 +1100

csiph-web