Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #97823
| Newsgroups | comp.lang.python |
|---|---|
| Date | 2015-10-19 16:19 -0700 |
| References | <q3da2bplpbt2njpoojie8ogfo7te63lhn2@4ax.com> |
| Message-ID | <00fbd018-4188-40da-bacf-394d762c9bed@googlegroups.com> (permalink) |
| Subject | Re: variable scope of class objects |
| From | sohcahtoa82@gmail.com |
On Monday, October 19, 2015 at 11:39:59 AM UTC-7, JonRob wrote:
> 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
Class variables are accessible without creating an instance of a class. Also, changing the value of a class variable affects ALL instances of that class. This is because the variable belongs to the class itself, not any of the instances of that class.
"self" is used to tell the interpreter that the variable/function you are accessing is a member of an instance of that class.
Here's an example:
class MyObject(object):
count = 0
def __init__(value):
MyObject.count += 1
self.value = value
def printStuff():
print("My value is ", self.value)
print(MyObject.count) # This will print 0
a = MyObject('a')
print(MyObject.count) # This will print 1
print(a.count) # This will also print 1
a.printStuff() # This will print "My value is a"
b = MyObject('b')
print(a.count) # This will print 2
print(b.count) # This will also print 2
b.printStuff() # This will print "My value is b"
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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