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


Groups > comp.lang.python > #97878

Re: variable scope of class objects

From Luca Menegotto <otlucaDELETE@DELETEyahoo.it>
Newsgroups comp.lang.python
Subject Re: variable scope of class objects
Date 2015-10-22 07:55 +0200
Organization Aioe.org NNTP Server
Message-ID <n09tor$ftt$1@speranza.aioe.org> (permalink)
References <q3da2bplpbt2njpoojie8ogfo7te63lhn2@4ax.com> <n04m96$tvd$1@speranza.aioe.org> <9ocd2btlkq7kp3margtn4sj3mehd7bpimm@4ax.com>

Show all headers | View raw


Il 20/10/2015 23:33, JonRob ha scritto:
>
>
> Hello Luca,
>
> I very much appreciated your comments.  And I understand the
> importance of "doing something right"  (i.e. convention).
>
> This leads me to another question.
>
> Because I am interfacing with an I2C sensor I have many register
> definations to include (30 register addresses and  26 Variables to be
> red from some of those registers.
> In your comment you mentioned that convention is to declare variables
> (and constants?)  in the construction (__ini__).
> I am concerned that the sheer number of varialbe / constants would
> make it difficult to read.
>
> In your opinion, what would be the best method to structure such code?
>
> Regards
> JonRob

Let's start from constants. Constants, in Python, simply don't exist 
(and IMHO this is one of the few lacks of Python). All you can do is to 
declare a variable and treat it as a constant: you never change it!

It doesn't make sense to put a constant declaration at instance level, 
declaring it in the __init__ part of a class. After all, a constant is 
an information you want to share. The choice is up to you as the project 
manager: if you think that your constant is deeply related to the class 
you're designing, declare it as a class variable; otherwise, declare it 
at global level (in this case, often I use a separate file dedicated to 
constant declaration).

-- 
Ciao!
Luca

Back to comp.lang.python | Previous | NextPrevious in thread | Next 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