variable scope of class objects

2015-10-19 Thread JonRob
Hi,

I've having trouble understanding the self concept as it applies to
variables.  I think I understand how it affects methods.

I haven't been able to fully grasp the scope of class variables and
the effect of the "self"  to the scope of the variable.

I (think) I understand that in the below case, the word self could be
replaced with "BME280" to explicitly call out a variable.

But even still I don't know how explicit call out effects the scope of
a variable.

The below pseudo code is distilled from my 1st attempt at a functional
Python program on the RasPi.

My questions are:
What is the scope of class variables?
does the self. prefix modify this scope?

Thanks

Regards

JonRob




#!/usr/bin/python
# -- developed using Python 2.7.3

class BME280:

# all the below are class variables
# those preceded by an underscore are predefined to some constant
# those without the underscore are to be "working" variables.

_regT1   = 0x88
_regH6   = 0xE7
_coeff_P2= 0x82
_coeff_P6= 0x32

filter   = 0#should these be "self"?
t_fine   = 0

def __init__(self, address=0x76, debug=True):
self.i2c = Adafruit_I2C(address)
self.address = address
self.debug = debug

def pressure_calc(self):
var1 = self.i2c.readU16(self._regT1,False)
p = (1048576.0 - var1) * _coeff_P2
return p

def read_pressure(self):  #called  by main application
pressure_hPa = pressure_calc(self) /10 
# apply compensation
return pressure_hPa

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: variable scope of class objects

2015-10-20 Thread JonRob
Thanks to all who replied to my question.   I received a lot of
information and points of view that are very helpful.   I realize some
of you folks spent more that a few minutes.  I really appreciate your
time.

Pardon me that i replied to random832's post and not the original but
my original was lost while I was trying to bookmark it.


Regards,
JonRob



On Mon, 19 Oct 2015 15:01:14 -0400, Random832 
wrote:

>[email protected] writes:
>>
>> The below pseudo code is distilled from my 1st attempt at a functional
>> Python program on the RasPi.
>>
>> My questions are:
>> What is the scope of class variables?
>
>You must access them as members of the class or an instance of the class.
>
>> does the self. prefix modify this scope?
>
>self just refers to the instance of the class that the function was
>called with. It can be any name.
>
>Python automatically transforms any reference to "[object].func" into a
>function (specifically, a bound method object) that will prefix [object]
>to the argument list of the defined function.
>
>> #!/usr/bin/python
>> # -- developed using Python 2.7.3
>>
>> class BME280:
>>
>> # all the below are class variables
>> # those preceded by an underscore are predefined to some constant
>
>Constants should be in uppercase, not prefixed with an underscore.
>
>Names prefixed with an underscore imply that they are "private" (not
>really) and therefore other code should not use them directly
>
>> # those without the underscore are to be "working" variables.
>
>I don't know what you mean by "working".
>
>>
>> _regT1   = 0x88
>> _regH6   = 0xE7
>> _coeff_P2= 0x82
>> _coeff_P6= 0x32
>> 
>> filter   = 0#should these be "self"?
>> t_fine   = 0
>
>I don't know, should they? If so they need to be in __init__.
>
>You haven't provided any functions that use them, so it's not clear what
>they're for.
>
>> 
>> def __init__(self, address=0x76, debug=True):
>> self.i2c = Adafruit_I2C(address)
>> self.address = address
>> self.debug = debug
>> 
>> def pressure_calc(self):
>> var1 = self.i2c.readU16(self._regT1,False)
>> p = (1048576.0 - var1) * _coeff_P2
>> return p
>> 
>> def read_pressure(self):  #called  by main application
>> pressure_hPa = pressure_calc(self) /10 
>> # apply compensation
>> return pressure_hPa
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: variable scope of class objects

2015-10-20 Thread JonRob


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




On Tue, 20 Oct 2015 08:17:16 +0200, Luca Menegotto
 wrote:

>Il 19/10/2015 20:39, JonRob ha scritto:
>
>> I (think) I understand that in the below case, the word self could be
>> replaced with "BME280" to explicitly call out a variable.
>>
>> But even still I don't know how explicit call out effects the scope of
>> a variable.
>
>These two statements make me think you come from C++ or something similar.
>
>In Python you can declare variables at class level, but this declaration 
>must NOT be interpreted in the same manner of a similar declaration in 
>C++: they remain at the abstract level of a class, and they have nothing 
>to do with an instance of a class (in fact, to be correctly invoked, 
>they must be preceeded by the class name).
>
>'self' (or a similar representation, you could use 'this' without 
>problem) gives you access to the instance of the class, even in the 
>constructor; it is important, because the constructor is the place where 
>instance variables should be defined. Something like this:
>
>class foo:
> # invoke with foo._imAtClassLevel
> _imAtClassLevel = 10
>
> def __init__(self):
> #  need to say how this must be invoked?
> self._imAtInstanceLevel = 0
>
>no confusion is possible, because:
>
>class foo2:
> _variable = 1000
>
> def __init__(self):
> # let's initialize an instance variable with
> # a class variable
> self._variable = foo2._variable
>
>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).
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: variable scope of class objects

2015-10-21 Thread JonRob
@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
 wrote:

>On Tue, 20 Oct 2015 17:33:21 -0400, [email protected] 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)
>
>
>   
>   
-- 
https://mail.python.org/mailman/listinfo/python-list