Path: csiph.com!news.mixmin.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!nzpost1.xs4all.net!not-for-mail Return-Path: 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; 'assignment': 0.07; 'definitions.': 0.07; 'executed': 0.07; 'statically': 0.07; '__init__': 0.09; 'bindings': 0.09; 'foo,': 0.09; 'later)': 0.09; 'obj': 0.09; "object's": 0.09; 'sfxlen:2': 0.10; 'python': 0.10; 'read.': 0.13; 'def': 0.13; 'suggest': 0.15; 'variables': 0.15; '(rather': 0.16; 'assignments': 0.16; 'class)': 0.16; 'constants': 0.16; 'dictionary.': 0.16; 'email addr:mail.python.org': 0.16; 'executed.': 0.16; 'from:addr:python': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'require.': 0.16; 'scope.': 0.16; 'sheer': 0.16; 'subject:class': 0.16; 'subject:variable': 0.16; 'values:': 0.16; 'wrote:': 0.16; 'script.': 0.18; 'variable': 0.18; '(see': 0.20; 'to:2**1': 0.21; 'body,': 0.22; '(or': 0.23; 'this:': 0.23; 'import': 0.24; 'header:In-Reply- To:1': 0.24; 'module': 0.25; 'script': 0.25; 'header:User- Agent:1': 0.26; 'define': 0.27; 'to:no real name:2**1': 0.27; 'function': 0.28; 'values': 0.28; 'raise': 0.29; 'convention': 0.30; 'another': 0.32; 'skip:_ 10': 0.32; 'received:84': 0.32; 'getting': 0.33; 'point': 0.33; 'class': 0.33; 'foo': 0.33; 'languages': 0.34; 'structure': 0.34; 'done': 0.35; 'exist': 0.35; 'something': 0.35; 'level': 0.35; 'comment': 0.35; 'but': 0.36; '(and': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'method': 0.37; 'skip:v 20': 0.38; 'along': 0.39; 'received:192': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'some': 0.40; 'your': 0.60; 'address': 0.61; 'default': 0.61; 'charset:windows-1252': 0.62; 'construction': 0.72; '"spam"': 0.84; "'foo'": 0.84 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=MbeRwMLf c=1 sm=1 tr=0 a=PwHmTYxeCAeSJvD9iABiLw==:117 a=PwHmTYxeCAeSJvD9iABiLw==:17 a=0Bzu9jTXAAAA:8 a=EBOSESyhAAAA:8 a=N659UExz7-8A:10 a=8AHkEIZyAAAA:8 a=KUSXJpJLPQFlrDdZ5z8A:9 a=w23nUMbXGA6zFQH9:21 a=tZjpBa6KwgAL6kyT:21 a=pILNOxqGKmIA:10 Subject: Re: variable scope of class objects To: JonRob@mail.python.org, python-list@python.org References: <9ocd2btlkq7kp3margtn4sj3mehd7bpimm@4ax.com> From: Erik Date: Tue, 20 Oct 2015 23:17:55 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.3.0 MIME-Version: 1.0 In-Reply-To: <9ocd2btlkq7kp3margtn4sj3mehd7bpimm@4ax.com> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Mailman-Approved-At: Thu, 22 Oct 2015 09:44:13 +0200 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 57 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1445499854 news.xs4all.nl 23836 [2001:888:2000:d::a6]:53811 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:97881 On 20/10/15 22:33, JonRob@mail.python.org wrote: > In your comment you mentioned that convention is to declare variables > (and constants?) in the construction (__ini__). I would suggest that 'constants' are not 'declared' in the __init__ method body, but either as class variables or (see later) at module scope. > I am concerned that the sheer number of varialbe / constants would > make it difficult to read. Remember that "instance variables" in Python are just name bindings to another object which are placed in the object's dictionary. Python is not like some other languages where you declare your object's member variables statically and they all magically exist when you create the object (whether you initialise them or not): none of the bindings exist until an assignment is executed. Therefore, you _must_ have a set of assignments which are executed to create the object with the bindings (or "instance variables") that you require. This is _usually_ done in __init__ with a set of assignments to 'self' - either using default values or values passed in to __init__. You may be getting to the point where the best way to structure this is to write your own module (rather than just a class) which you then import from your main script. For example, you might do something like this: mymodule.py: CONSTANT_X = 0x99 CONSTANT_Y = 0xF00 CONSTANT_Z = "Spam" class MyObject(object): def __init__(self, foo, bar): # 'foo' can be anything. 'bar' must be one of # a specific set of values: if bar not in (CONSTANT_X, CONSTANT_Y): raise ValueError("Parameter 'bar'") self.foo = foo self.bar = bar Then, in your main script you might do something like: import mymodule obj = mymodule.MyObject(100, mymodule.CONSTANT_X) ... then start calling methods on 'obj'. So you just define your address and variable constants at module level and import them along with any class and function definitions. E.