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


Groups > comp.lang.python > #97881

Re: variable scope of class objects

Subject Re: variable scope of class objects
References <q3da2bplpbt2njpoojie8ogfo7te63lhn2@4ax.com> <n04m96$tvd$1@speranza.aioe.org> <9ocd2btlkq7kp3margtn4sj3mehd7bpimm@4ax.com>
From Erik <python@lucidity.plus.com>
Date 2015-10-20 23:17 +0100
Newsgroups comp.lang.python
Message-ID <mailman.83.1445499854.878.python-list@python.org> (permalink)

Show all headers | View raw


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.

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


Thread

variable scope of class objects JonRob - 2015-10-19 14:39 -0400
  Re: variable scope of class objects Random832 <random832@fastmail.com> - 2015-10-19 15:01 -0400
    Re: variable scope of class objects JonRob - 2015-10-20 17:11 -0400
  Re: variable scope of class objects sohcahtoa82@gmail.com - 2015-10-19 16:19 -0700
    Re: variable scope of class objects Terry Reedy <tjreedy@udel.edu> - 2015-10-19 20:03 -0400
  Re: variable scope of class objects Nagy László Zsolt <gandalf@shopzeus.com> - 2015-10-20 07:31 +0200
  Re: variable scope of class objects Luca Menegotto <otlucaDELETE@DELETEyahoo.it> - 2015-10-20 08:17 +0200
    Re: variable scope of class objects Nagy László Zsolt <gandalf@shopzeus.com> - 2015-10-20 08:38 +0200
      Re: variable scope of class objects Luca Menegotto <otlucaDELETE@DELETEyahoo.it> - 2015-10-20 09:23 +0200
    Re: variable scope of class objects JonRob - 2015-10-20 17:33 -0400
      Re: variable scope of class objects Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-10-20 20:18 -0400
        Re: variable scope of class objects JonRob - 2015-10-21 19:35 -0400
          Re: variable scope of class objects Luca Menegotto <otlucaDELETE@DELETEyahoo.it> - 2015-10-22 11:59 +0200
      What does it mean for Python to have “constants”? (was: variable scope of class objects) Ben Finney <ben+python@benfinney.id.au> - 2015-10-21 11:27 +1100
      Re: What does it mean for Python to have “constants”? Nagy László Zsolt <gandalf@shopzeus.com> - 2015-10-21 08:13 +0200
      Re: variable scope of class objects Luca Menegotto <otlucaDELETE@DELETEyahoo.it> - 2015-10-22 07:55 +0200
      Re: variable scope of class objects Erik <python@lucidity.plus.com> - 2015-10-20 23:17 +0100

csiph-web