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


Groups > comp.lang.python > #102356

Re: carry **arguments through different scopes/functions

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Joel Goldstick <joel.goldstick@gmail.com>
Newsgroups comp.lang.python
Subject Re: carry **arguments through different scopes/functions
Date Sun, 31 Jan 2016 08:12:27 -0500
Lines 101
Message-ID <mailman.167.1454246289.2338.python-list@python.org> (permalink)
References <3ptWgs1F6Vz5vN2@dovecot03.posteo.de>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de UZK8/uYdII2gam+HFeYmVgPhdzPoJgLLM+Sks+blQtJQ==
Return-Path <joel.goldstick@gmail.com>
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; 'true,': 0.04; 'none,': 0.05; 'python3': 0.05; 'sys': 0.05; 'false,': 0.07; 'false.': 0.07; 'cc:addr:python-list': 0.09; 'global,': 0.09; 'globals': 0.09; 'simplified': 0.09; 'subtle': 0.09; 'python': 0.10; 'python.': 0.11; 'jan': 0.11; 'question.': 0.13; 'def': 0.13; 'variables': 0.15; '2016': 0.16; 'false:': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'reproduce': 0.16; 'scopes': 0.16; 'true:': 0.16; 'variable.': 0.16; 'wrote:': 0.16; 'skip:` 10': 0.18; 'variable': 0.18; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'fix': 0.21; '31,': 0.22; 'own.': 0.22; 'am,': 0.23; 'this:': 0.23; 'import': 0.24; 'written': 0.24; 'header:In-Reply-To:1': 0.24; 'example': 0.26; 'coding': 0.27; 'mostly': 0.27; 'skip:# 10': 0.27; 'message-id:@mail.gmail.com': 0.27; 'back.': 0.27; 'function': 0.28; 'subject:/': 0.30; 'that.': 0.30; 'url:mailman': 0.30; 'code': 0.30; 'skip:[ 10': 0.31; 'skip:_ 10': 0.32; 'skip:. 10': 0.32; 'topic': 0.32; 'maybe': 0.33; 'problem': 0.33; 'url:python': 0.33; 'url:listinfo': 0.34; 'quickly': 0.34; 'received:google.com': 0.35; 'could': 0.35; 'false': 0.35; 'but': 0.36; 'should': 0.36; 'there': 0.36; 'url:org': 0.36; 'received:209.85': 0.36; 'skip:{ 10': 0.36; 'structures': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'received:209.85.213': 0.37; 'things': 0.38; 'received:209': 0.38; 'data': 0.39; 'sure': 0.39; 'does': 0.39; "didn't": 0.39; 'url:mail': 0.40; 'some': 0.40; 'bring': 0.62; 'making': 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; 'to:none': 0.91; 'joel': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=joKzMUJ69+pMQ8y2LHFdUZWbw08W3Gx7vflttVaGFKg=; b=VLP7n7ppcg6lvPnpe0IzR0b1qyMdCRGvjDkuHyPG16FaortJZZBNJf/8TePHJ8sQVK xr2p2KydPJ8f+Jzg5HIxxKiFWb7y2amCyWdL9Um4D+eKnHuflC91m5w3ose6/HjgL7pf 6JtpjsyYeZsxFxR+LHA+mep9T59od7vNhgdB5Fg8Ts7ltBVjBRYMSKDg+A/cNXBX7bRq DOZkjD68FUDeh/Z7mrcN1f6dDmiKevMAMQvfZ4yRHQ4YDZWwLQIHPBZH/G3CO+VN5b8V MqyZRoZmFvJ77/RSS6lj+TIBzMx5L+O9d0j8iWOlBOI2y/TV6l+NNnnqcQnGZw9Cpxa4 BZTQ==
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:cc:content-type; bh=joKzMUJ69+pMQ8y2LHFdUZWbw08W3Gx7vflttVaGFKg=; b=DuZKKcNSck53dIh7n8HhlB8YDEhsz6LcCEnm+vduVc6+dBfSiikipfkaqB+LW/OCPz 4yTMJJWYRI8CYj03Ck4+YFpGd6sNpX4Rsan4+2phPQLaL0XPtOD2L1ePM/9fziIenTWO mos4eKPS6Ku+ywvQp+iwrePg1FzqEIEVXlHUe/IBSV5Gs4azPu+X7JmXIKuqR9YUM2uT yX9wCtcsaNJD6GEGJCmaxVhTY8zHpKLStSnFiKM0yhew+yYE1q7qcnV4DRaXld4oKNGO T6uY+HijgEc/w+ehU977L8cdT9wqzvBKYnvdq264knsqhgzQduzY/xHNLaIYd6M+d/uG +/7Q==
X-Gm-Message-State AG10YOTb3RQkdh962Xklt98y++f0f3++Hql2RE6sMmOFNl0m3w+3f+m5B4mu4mJzc33c80ZOrFyutMMSggXiPQ==
X-Received by 10.50.61.145 with SMTP id p17mr6206046igr.28.1454245947328; Sun, 31 Jan 2016 05:12:27 -0800 (PST)
In-Reply-To <3ptWgs1F6Vz5vN2@dovecot03.posteo.de>
X-Content-Filtered-By Mailman/MimeDel 2.1.20+
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:102356

Show key headers only | View raw


On Sun, Jan 31, 2016 at 7:19 AM, <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
>

The above returns None, not False.  Maybe you want False, so return False.
However, when you call this function below, you set return_code = True, so
it will never be False

>
>         if one_id == 7:
>             return_code = True
>         else:
>             return_code = False
>

The above could be just:
           return one_id == 7

no need for if/else

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

This will always return True because you set return_code = True and never
change it.  You might want this:

          return_code = walkOn_ids(...

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

Before you start randomly making variables global, make sure you have
written code that does what you want it to.  Globals to fix bad coding will
quickly bring a big mess.  Of course its more subtle than this, but mostly
globals are a very bad idea.

> --
> https://mail.python.org/mailman/listinfo/python-list
>



-- 
Joel Goldstick
http://joelgoldstick.com/stats/birthdays

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


Thread

Re: carry **arguments through different scopes/functions Joel Goldstick <joel.goldstick@gmail.com> - 2016-01-31 08:12 -0500

csiph-web