variable scope of class objects

Luca Menegotto otlucaDELETE at DELETEyahoo.it
Thu Oct 22 01:55:40 EDT 2015


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





More information about the Python-list mailing list