Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #99753
| Path | csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Terry Reedy <tjreedy@udel.edu> |
| Newsgroups | comp.lang.python |
| Subject | Re: static variables |
| Date | Mon, 30 Nov 2015 12:38:40 -0500 |
| Lines | 35 |
| Message-ID | <mailman.46.1448905207.14615.python-list@python.org> (permalink) |
| References | <n3i06o$9ot$1@news2.informatik.uni-stuttgart.de> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=utf-8; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-Trace | news.uni-berlin.de m++UltiBjx8Il6bni3j0Sg2whcuM/kyTAlTygLvFW1Vg== |
| 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; 'none:': 0.05; 'main()': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'jan': 0.11; 'def': 0.13; 'a()': 0.16; 'locals():': 0.16; 'main():': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'reedy': 0.16; 'wrote:': 0.16; 'attribute': 0.18; 'variable': 0.18; 'function:': 0.22; 'code,': 0.23; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'implement': 0.32; 'run': 0.33; 'but': 0.36; 'received:71': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'to:addr:python.org': 0.40; 'expect.': 0.84; 'received:fios.verizon.net': 0.91 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| X-Gmane-NNTP-Posting-Host | pool-71-185-227-36.phlapa.fios.verizon.net |
| User-Agent | Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.4.0 |
| In-Reply-To | <n3i06o$9ot$1@news2.informatik.uni-stuttgart.de> |
| 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:99753 |
Show key headers only | View raw
On 11/30/2015 12:15 PM, Ulli Horlacher wrote:
>
> I try to to implement a "static variable" inside a function:
>
> def main():
> a(1)
> a(2)
> a()
> print(a.x)
> if 'a.x' in globals(): print('global variable')
> if 'a.x' in locals(): print('local variable')
>
> def a(x=None):
> if not x is None: a.x = x
> print(':',a.x)
>
> main()
>
> When I run this code, I get:
>
> : 1
> : 2
> : 2
> 2
>
> This is exactly what I expect.
> But what is a.x?
> It is neither a variable in globals() nor in locals()
a.x is an attribute of the object a.
--
Terry Jan Reedy
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
static variables Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2015-11-30 17:15 +0000
Re: static variables Terry Reedy <tjreedy@udel.edu> - 2015-11-30 12:38 -0500
Re: static variables BartC <bc@freeuk.com> - 2015-11-30 20:32 +0000
Re: static variables Steven D'Aprano <steve@pearwood.info> - 2015-12-01 12:01 +1100
Re: static variables Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2015-12-01 08:26 +0000
Re: static variables Peter Otten <__peter__@web.de> - 2015-12-01 10:01 +0100
Re: static variables Grobu <snailcoder@retrosite.invalid> - 2015-12-01 10:15 +0100
Re: static variables Steven D'Aprano <steve@pearwood.info> - 2015-12-02 12:02 +1100
Re: static variables Erik <python@lucidity.plus.com> - 2015-12-02 01:16 +0000
Re: static variables Steven D'Aprano <steve@pearwood.info> - 2015-12-02 12:24 +1100
Re: static variables Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2015-12-02 09:21 +0100
Re: static variables Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2015-12-02 09:34 +0100
Re: static variables Steven D'Aprano <steve@pearwood.info> - 2015-12-02 21:22 +1100
Re: static variables Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2015-12-02 12:09 +0100
Re: static variables Steven D'Aprano <steve@pearwood.info> - 2015-12-03 00:11 +1100
Re: static variables Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2015-12-02 14:41 +0100
Re: static variables Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-12-02 13:48 +0000
Re: static variables Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2015-12-02 15:07 +0100
Re: static variables Ian Kelly <ian.g.kelly@gmail.com> - 2015-12-02 08:15 -0600
Re: static variables Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-12-02 14:31 +0000
Re: static variables Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2015-12-02 16:30 +0100
Re: static variables Ian Kelly <ian.g.kelly@gmail.com> - 2015-12-02 14:30 -0600
Re: static variables Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2015-12-03 10:06 +0100
Re: static variables Chris Angelico <rosuav@gmail.com> - 2015-12-02 20:23 +1100
Re: static variables Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2015-12-02 10:47 +0100
Re: static variables Marko Rauhamaa <marko@pacujo.net> - 2015-12-02 12:18 +0200
Re: static variables Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2015-12-02 11:56 +0100
Re: static variables Marko Rauhamaa <marko@pacujo.net> - 2015-12-02 13:30 +0200
Re: static variables Steven D'Aprano <steve@pearwood.info> - 2015-12-03 00:17 +1100
Re: static variables Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> - 2015-12-01 10:58 +0100
Re: static variables Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2015-12-01 11:47 +0000
csiph-web