variable scope of class objects

JonRob JonRob
Tue Oct 20 17:11:14 EDT 2015


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 <random832 at fastmail.com>
wrote:

>JonRob at mail.python.org 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



More information about the Python-list mailing list