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


Groups > comp.lang.python > #3565

Re: Namespaces in functions vs classes

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.dougwise.org!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <gerald.britton@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.005
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'python': 0.07; 'attribute': 0.09; 'definitions.': 0.09; 'namespace': 0.09; 'def': 0.13; '%s!"': 0.16; 'function)': 0.16; 'jumping': 0.16; 'qualified.': 0.16; 'subject:Namespaces': 0.16; 'subject:classes': 0.16; 'to:name:python-list (general)': 0.16; 'question.': 0.18; 'to:2**1': 0.20; 'variable': 0.21; 'received:209.85.220': 0.26; "i'm": 0.26; 'function': 0.27; 'message-id:@mail.gmail.com': 0.28; 'thanks': 0.29; 'helpful': 0.29; 'definition': 0.29; 'class': 0.29; 'discussion.': 0.31; 'whereas': 0.31; 'does': 0.31; 'separate': 0.31; 'to:addr:python-list': 0.32; 'uses': 0.34; 'print': 0.35; 'getting': 0.36; 'think': 0.36; 'should': 0.37; 'received:209.85': 0.37; '(to': 0.38; 'received:google.com': 0.38; 'but': 0.38; 'unless': 0.38; 'to:addr:python.org': 0.39; 'received:209': 0.39; 'header:Received:5': 0.40; 'back': 0.61; 'as:': 0.64
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:date:message-id:subject:from:to :content-type; bh=W1X3wqepvpcMNDnwr0bdg6m8Znzile9UM2hhX2sg0H8=; b=FskiIzH5YFQHSj2DoV4E954YvoI8KxlkDmuLZ7PFkRE1AIPM8MM5duUZjARV2QOY+5 p3HLDleAAZRF6kB1XAh7VWzSuRyt3/hjiwXtwyHH7SFNODIgNEcbwh4HTfycENXNTiYO Qq3nMDBt02rzTPebYSssBA4UHCT538T5Vsr70=
DomainKey-Signature a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:date:message-id:subject:from:to:content-type; b=enA8BbpkAVBXgAymJ7yP1oXl6MzM/DsLkOiJgROGFxY08bUXtkrQg/CWnsfFPQruyw gUJr0GTOak4tnZUjnMrjmCDN5+XNHUci66aGF5/9oXqw4YQoMmQ53v3B+aDMEmXteLut DNHVvqaGu+weACQyHDfSFp0Wgq5s6amciIEq4=
MIME-Version 1.0
Date Tue, 19 Apr 2011 10:58:21 -0400
Subject Re: Namespaces in functions vs classes
From Gerald Britton <gerald.britton@gmail.com>
To Ethan Furman <ethan@stoneleaf.us>, "python-list (General)" <python-list@python.org>
Content-Type text/plain; charset=ISO-8859-1
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
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.563.1303225104.9059.python-list@python.org> (permalink)
Lines 43
NNTP-Posting-Host 82.94.164.166
X-Trace 1303225104 news.xs4all.nl 41110 [::ffff:82.94.164.166]:40910
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:3565

Show key headers only | View raw


Ethan -- I'm just getting back to this question.  If you recall, you asked:

[snip]

8<----------------------------------------------------
"script with possible name clashes"

eggs = 'scrambled eggs'
meat = 'steak'

class Breakfast():
     meat = 'spam'
     def serve(self):
         print("Here's your %s and %s!" %
                (eggs, meat))

Breakfast().serve()
8<----------------------------------------------------

>What will serve print?

Well, I think it should print the same thing as:

def Breakfast():
    meat = 'spam'
    def serve(self):
        print("Here's your %s and %s!" %
               (eggs, meat))
    return serve(None)

Breakfast()

but it doesn't!  The function definition uses the local (to the
function) variable "meat", whereas the class method uses the global
definition of "meat".  The class attribute "meat" is not seen by the
serve method unless it is qualified.  I now understand the Python does
not consider a class definition as a separate namespace as it does for
function definitions.  That is a helpful understanding.

Anyway, thanks for jumping in to the discussion.

-- 
Gerald Britton

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


Thread

Re: Namespaces in functions vs classes Gerald Britton <gerald.britton@gmail.com> - 2011-04-19 10:58 -0400

csiph-web