Path: csiph.com!eternal-september.org!feeder.eternal-september.org!aioe.org!.POSTED!not-for-mail From: Luca Menegotto Newsgroups: comp.lang.python Subject: Re: variable scope of class objects Date: Thu, 22 Oct 2015 07:55:40 +0200 Organization: Aioe.org NNTP Server Lines: 40 Message-ID: References: <9ocd2btlkq7kp3margtn4sj3mehd7bpimm@4ax.com> NNTP-Posting-Host: jyK346aZFHsQ6PZHWmWRng.user.speranza.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@aioe.org User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.3.0 X-Notice: Filtered by postfilter v. 0.8.2 Xref: csiph.com comp.lang.python:97878 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