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


Groups > comp.lang.python > #97875

Re: variable scope of class objects

From JonRob
Newsgroups comp.lang.python
Subject Re: variable scope of class objects
Date 2015-10-21 19:35 -0400
Message-ID <9i7g2b17d3e54cbcf35digbod9216fnovl@4ax.com> (permalink)
References <q3da2bplpbt2njpoojie8ogfo7te63lhn2@4ax.com> <n04m96$tvd$1@speranza.aioe.org> <9ocd2btlkq7kp3margtn4sj3mehd7bpimm@4ax.com> <mailman.64.1445386763.878.python-list@python.org>

Show all headers | View raw


@Dennis,


Thanks for your example.  My structure is very similar.  Perhaps I was
reading too much into Luca's below statement regarding declaring
variables.

Regards,
JonRob




    Luca wrote...
>Please, note that declaring a variable in the constructor is only a 
>convention: in Python you can add a variable to an object of a class 
>wherever you want in your code (even if it is very dangerous and 
>discouraged).


 





On Tue, 20 Oct 2015 20:18:35 -0400, Dennis Lee Bieber
<wlfraed@ix.netcom.com> wrote:

>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)
>
>
>			
>		

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