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


Groups > comp.lang.python > #29992

Re: which a is used?

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin1!goblin.stu.neva.ru!news2.euro.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <dwightdhutto@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; 'python,': 0.02; 'caller': 0.07; 'variables.': 0.07; 'python': 0.09; 'behave': 0.09; 'called.': 0.09; 'defined.': 0.09; 'len,': 0.09; 'received:mail- vb0-f46.google.com': 0.09; 'sep': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; '24,': 0.16; 'a():': 0.16; 'global.)': 0.16; 'lexical': 0.16; 'scope,': 0.16; 'subject:which': 0.16; 'to:addr:pearwood.info': 0.16; 'to:addr:steve+comp.lang.python': 0.16; "to:name:steven d'aprano": 0.16; 'uses,': 0.16; 'mon,': 0.16; 'wrote:': 0.17; 'code,': 0.18; 'received:209.85.212.46': 0.18; '(or': 0.18; 'variable': 0.20; 'changes': 0.20; 'equivalent': 0.20; 'all,': 0.21; 'suddenly': 0.22; 'defined': 0.22; 'cc:2**0': 0.23; 'cc:no real name:2**0': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'message-id:@mail.gmail.com': 0.27; 'received:209.85.212': 0.28; '-0700,': 0.29; 'accidentally': 0.29; 'behaviour': 0.29; "d'aprano": 0.29; 'prints': 0.29; 'relies': 0.29; 'steven': 0.29; 'url:mailman': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; 'function': 0.30; 'code': 0.31; 'url:python': 0.32; 'could': 0.32; 'print': 0.32; 'url:listinfo': 0.32; 'correctly.': 0.33; 'received:google.com': 0.34; 'third': 0.34; 'built-in': 0.35; 'follows:': 0.35; 'especially': 0.35; 'pm,': 0.35; 'subject:?': 0.35; 'received:209.85': 0.35; 'but': 0.36; 'url:org': 0.36; 'should': 0.36; 'does': 0.37; 'uses': 0.37; 'being': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'called': 0.39; 'where': 0.40; 'header:Received:5': 0.40; 'url:mail': 0.40; 'think': 0.40; 'matter': 0.61; 'more': 0.63; 'within': 0.64; 'results': 0.65; '20,': 0.65; 'dear': 0.66; 'why?': 0.84; 'tricky': 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:to :cc:content-type; bh=lL0Eqt+xBInofT5tYz2MuIOEGkdRsSBom8LUnWAWpG4=; b=wDIQ3wzADHE4s7zGkHusSEell+MUyuLmHK9o66aFexB2+qfncbe3A2oY7B4xfM9yf/ jqn/c5D3nJHJsOGtkmhHMBHNKVPUI/W8JKlf5iRestwsQwMX+2wVOJ1t96M9yLvvPPE0 VJaY6mMkZS1Oc4TXUkVrH+ERSgoCrp3Mmb+K66H2g4sl+AuTmWdu2ePrYvBBW2R7LbM0 5SQNVJ+vtZqO7BhZC89Lg/pMgIPUk+g9KDmsQ1mYdytv3+cJEnqZAbZ2LADH6N4PXXK/ ICWLvKZoS2TJb8fOYkdm+zBH21YHEectogTuiXxgjhfyJZ2o2YzVpMnuL5SOyJYHyN2f cOPg==
MIME-Version 1.0
In-Reply-To <50610671$0$29981$c3e8da3$5496439d@news.astraweb.com>
References <95e88ca6-36ad-4820-8ecc-c9a788517430@googlegroups.com> <50610671$0$29981$c3e8da3$5496439d@news.astraweb.com>
Date Mon, 24 Sep 2012 21:47:04 -0400
Subject Re: which a is used?
From Dwight Hutto <dwightdhutto@gmail.com>
To "Steven D'Aprano" <steve+comp.lang.python@pearwood.info>
Content-Type text/plain; charset=ISO-8859-1
Cc python-list@python.org
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 <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1263.1348537627.27098.python-list@python.org> (permalink)
Lines 82
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1348537627 news.xs4all.nl 6850 [2001:888:2000:d::a6]:56694
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:29992

Show key headers only | View raw


On Mon, Sep 24, 2012 at 9:18 PM, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:
> On Mon, 24 Sep 2012 16:43:24 -0700, Jayden wrote:
>
>> Dear All,
>>
>> I have a simple code as follows:
>>
>> # Begin
>> a = 1
>>
>> def f():
>>     print a
>>Paul Rubin <no.email@nospam.invalid>
>> def g():
>>     a = 20
>>     f()
>>
>> g()
>> #End
>>
>> I think the results should be 20, but it is 1. Would you please tell me
>> why?
>
> You are expecting "dynamic scoping", Python uses "static scoping" (or
> lexical scoping). With lexical scoping, you can reason about the
> behavioPaul Rubin <no.email@nospam.invalid>ur of a function by knowing only how and where it is defined. The
> caller is irrelevant.
>
> Since fuPaul Rubin <no.email@nospam.invalid>nction f is defined globally, and does not have its own local
> variable a, it will always see the global variable a no matter where it
> is called. So when you call f() from inside g(), f prints 1, the global
> a, not 20, g's local a.
>
> While dynamic scoping has its uses, it is more tricky to use correctly.
> One can no longer understand the behaviour of a function just by reading
> the funcPaul Rubin <no.email@nospam.invalid>tion's own code, knowing where and how it is defined. You also
> need to know where it is called. A function f that works perfectly when
> you call it from functions g, h, k, ... will suddenly misbehave (crash,
> or worse, behave wrongly) when called from function v because v
> accidentally changes some global variable that f relies on.
>
> This is especially a danger for Python, because built-in functions like
> len, chr, ord, print (version 3 only), and many others are all global
> variables.
>
> (Technically, they are in a third scope, the builtins, but that's
> equivalent to being global.)
>

But within a class this is could be defined as self.x within the
functions and changed, correct?


class a():
	def __init__(self,a):
		self.a = a

	def f(self):
		print self.a

	def g(self):
		self.a = 20
		print self.a


a = a(1)
a.f()
a.g()


> --
> Steven
> --
> http://mail.python.org/mailman/listinfo/python-list



-- 
Best Regards,
David Hutto
CEO: http://www.hitwebdevelopment.com

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


Thread

which a is used? Jayden <jayden.shui@gmail.com> - 2012-09-24 16:43 -0700
  Re: which a is used? Dwight Hutto <dwightdhutto@gmail.com> - 2012-09-24 19:57 -0400
  Re: which a is used? Dwight Hutto <dwightdhutto@gmail.com> - 2012-09-24 20:06 -0400
  Re: which a is used? alex23 <wuwei23@gmail.com> - 2012-09-24 17:08 -0700
    Re: which a is used? Dwight Hutto <dwightdhutto@gmail.com> - 2012-09-24 21:13 -0400
      Re: which a is used? alex23 <wuwei23@gmail.com> - 2012-09-24 19:14 -0700
        Re: which a is used? Dwight Hutto <dwightdhutto@gmail.com> - 2012-09-24 22:37 -0400
          Re: which a is used? Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2012-09-25 07:07 +0200
            Re: which a is used? Dwight Hutto <dwightdhutto@gmail.com> - 2012-09-25 01:30 -0400
              Re: which a is used? alex23 <wuwei23@gmail.com> - 2012-09-25 05:48 -0700
            Re: which a is used? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-09-25 08:44 +0100
            Stop feeding the trolls (Was: which a is used?) D'Arcy Cain <darcy@druid.net> - 2012-09-25 09:53 -0400
            Re: Stop feeding the trolls (Was: which a is used?) David Robinow <drobinow@gmail.com> - 2012-09-25 21:00 -0400
            Re: Stop feeding the trolls (Was: which a is used?) Dwight Hutto <dwightdhutto@gmail.com> - 2012-09-26 03:06 -0400
              Re: Stop feeding the trolls (Was: which a is used?) alex23 <wuwei23@gmail.com> - 2012-09-26 17:40 -0700
                Re: Stop feeding the trolls Ben Finney <ben+python@benfinney.id.au> - 2012-09-27 13:06 +1000
                Re: Stop feeding the trolls Dwight Hutto <dwightdhutto@gmail.com> - 2012-09-27 23:39 -0400
                Re: Stop feeding the trolls (Was: which a is used?) Dwight Hutto <dwightdhutto@gmail.com> - 2012-09-27 23:47 -0400
                Re: Stop feeding the trolls (Was: which a is used?) Chris Angelico <rosuav@gmail.com> - 2012-09-28 14:40 +1000
                Re: Stop feeding the trolls (Was: which a is used?) Dwight Hutto <dwightdhutto@gmail.com> - 2012-09-28 00:50 -0400
                Re: Stop feeding the trolls (Was: which a is used?) "Littlefield, Tyler" <tyler@tysdomain.com> - 2012-09-27 23:12 -0600
                Re: Stop feeding the trolls (Was: which a is used?) Dwight Hutto <dwightdhutto@gmail.com> - 2012-09-28 01:21 -0400
                Re: Stop feeding the trolls (Was: which a is used?) rusi <rustompmody@gmail.com> - 2012-09-27 22:51 -0700
                Re: Stop feeding the trolls (Was: which a is used?) Dwight Hutto <dwightdhutto@gmail.com> - 2012-09-28 02:20 -0400
            Re: Stop feeding the trolls (Was: which a is used?) Dwight Hutto <dwightdhutto@gmail.com> - 2012-09-26 03:07 -0400
        Re: which a is used? "Colin J. Williams" <cjw@ncf.ca> - 2012-09-25 08:52 -0400
      Re: which a is used? Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2012-09-25 07:06 +0200
        Re: which a is used? Dwight Hutto <dwightdhutto@gmail.com> - 2012-09-25 01:22 -0400
          Re: which a is used? Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2012-09-25 07:39 +0200
    Re: which a is used? Terry Reedy <tjreedy@udel.edu> - 2012-09-25 01:05 -0400
  Re: which a is used? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-09-25 01:18 +0000
    Re: which a is used? Dwight Hutto <dwightdhutto@gmail.com> - 2012-09-24 21:47 -0400
      Re: which a is used? Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2012-09-25 07:06 +0200
    Re: which a is used? Dwight Hutto <dwightdhutto@gmail.com> - 2012-09-24 21:50 -0400
  Re: which a is used? Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2012-09-25 11:30 +0200

csiph-web