Path: csiph.com!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed8.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; '(python': 0.05; 'none,': 0.05; 'extracted': 0.07; '22,': 0.09; 'message-id:@4ax.com': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'question.': 0.13; 'read.': 0.13; 'def': 0.13; 'value.': 0.15; 'variables': 0.15; '%s,': 0.16; '...,': 0.16; '>in': 0.16; 'constants': 0.16; 'dictionaries': 0.16; 'email addr:mail.python.org': 0.16; 'hint': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'registers.': 0.16; 'sheer': 0.16; 'subject:class': 0.16; 'subject:variable': 0.16; 'later': 0.16; 'retrieval': 0.18; 'url:home': 0.18; 'language': 0.19; 'load': 0.20; '2015': 0.20; '%s"': 0.22; 'assuming': 0.22; 'constant': 0.22; 'level,': 0.22; 'defined': 0.23; 'module': 0.25; 'command': 0.26; 'header:X -Complaints-To:1': 0.26; 'appreciated': 0.27; 'values': 0.28; 'skip:( 20': 0.28; 'dictionary': 0.29; 'typically': 0.29; 'print': 0.30; 'code': 0.30; 'convention': 0.30; 'another': 0.32; 'skip:_ 10': 0.32; 'addresses': 0.32; 'returned': 0.32; 'maybe': 0.33; 'class': 0.33; 'tue,': 0.34; 'lists': 0.34; 'protocol': 0.35; 'something': 0.35; 'comment': 0.35; '(i.e.': 0.36; 'to:addr :python-list': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'received:org': 0.37; 'charset:us-ascii': 0.37; 'anything': 0.38; 'sure': 0.39; 'does': 0.39; 'to:addr:python.org': 0.40; 'some': 0.40; 'your': 0.60; 'skip:u 10': 0.61; 'more': 0.63; 'user,': 0.67; 'construction': 0.72; '.....': 0.76; 'capitals': 0.84; '(30': 0.91; 'dennis': 0.91; 'received:108': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Dennis Lee Bieber Subject: Re: variable scope of class objects Date: Tue, 20 Oct 2015 20:18:35 -0400 Organization: IISS Elusive Unicorn References: <9ocd2btlkq7kp3margtn4sj3mehd7bpimm@4ax.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: 108.68.178.61 X-Newsreader: Forte Agent 6.00/32.1186 X-No-Archive: YES 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: 77 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1445386763 news.xs4all.nl 23780 [2001:888:2000:d::a6]:41065 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:97849 On Tue, 20 Oct 2015 17:33:21 -0400, JonRob@mail.python.org declaimed the following: > > >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. > "Constants" are typically defined at module level, using all capitals as a hint to the reader (Python does not have anything that one might consider a true constant -- other than the language defined singletons: None, and maybe by now True and False). Register addresses are likely "constants". Not sure about your "26 Variables"... Do they map directly to registers, or are they extracted as fields from the values returned -- that is, a register may have two or more "variables"? Do you read ALL registers on command and hold the values (note my usage -- values can be held in lists or dictionaries using a single "variable") for later retrieval by the user, or only read A register on command by the user and return that value. -=-=-=-=- # registers for a fictitious motion sensor GYROXREG = 0x0010 GYROYREG = 0x0011 GYROZREG = 0x0001 ... MAGZREG = 0x0100 class SensorA(I2C): #I'm assuming class I2C provides read/write functions _registers = [GYROXREG, GYROYREG, GYROZREG, ..., MAGZREG] def __init__(self, SCLpin, SDApin, slaveAddress): self._SCL = SCLpin self._SDA = SDApin self._addr = slaveAddress self.update() #initial load of values def update(self): #basically a loop over all addresses #I'm not going to try to pseudo code the full I2C protocol self.values = {} #yes, a dictionary for reg in _registers: aValue = self.read(self._SCL, self._SDA, self._addr, reg) #inherited from I2C class self.values[reg] = aValue .... mySensor = SensorA(21, 22, 0x6) while True mySensor.update() print ("Gyro X: %s, Y: %s, Z: %s" % (mySensor.values[GYROXREG], mySensor.values[GYROYREG], mySensor.values[GYROZREG])) time.sleep(1.0) -- Wulfraed Dennis Lee Bieber AF6VN wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/