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


Groups > comp.lang.python > #97826

Re: variable scope of class objects

From Luca Menegotto <otlucaDELETE@DELETEyahoo.it>
Newsgroups comp.lang.python
Subject Re: variable scope of class objects
Date 2015-10-20 08:17 +0200
Organization Aioe.org NNTP Server
Message-ID <n04m96$tvd$1@speranza.aioe.org> (permalink)
References <q3da2bplpbt2njpoojie8ogfo7te63lhn2@4ax.com>

Show all headers | View raw


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

-- 
Ciao!
Luca

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