Use of private variables 
Author Message
 Use of private variables

-----BEGIN PGP SIGNED MESSAGE-----

Quote:

>Folks,... me again...

>I'm using Python1.4 under Win95
>I'm trying to use a private variable in a class definition, and it looks
>like something's wrong...
>class Path:
>  __sep='blahblah'
>  def method(self)
>    print 'this is the value of sep : '+__sep

>When running I get a NameError on _Path__sep  .....

>What am I doin' wrong ?

When you want to reference a variable in a class from within a method, you
need to qualify it with "self" (that's why self gets passed to all
methods).  So, your Path.method() should b:

  def method(self): # BTW, you missed the colon
    print 'this is the vlaue of sep :', self.__sep
          # also, use a comma with print, it'll concatenate it with a space

- --
Michael W. Ryan

PGP fingerprint: 7B E5 75 7F 24 EE 19 35  A5 DF C3 45 27 B5 DB DF

-----BEGIN PGP SIGNATURE-----
Version: 2.6.2
Comment: PGP signed with SigEd v1.3.1 - http://www.*-*-*.com/ ~pgregg/siged/

iQCVAwUBND0gw1OKVJ5ciELNAQEcBwP/U2re0lq3H3UyVAU4r5+bMaDn4OL/p84C
dTHi6+JwElUT+sL0U8ZaLYnFD/QHaSgnuqGVy9hTvF8yoQiwnw8r6wGtMPhyeQdd
x8T+MdE/z6s0X53d/RNO2J7597IAQp2923U3+sPZDEKwujKqNAv3jhPf24rOGYYP
DMsudFoc1hk=
=vfMb
-----END PGP SIGNATURE-----



Mon, 27 Mar 2000 03:00:00 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. PRIVATE variables (was parameters)

2. Lots of Public and Private variables - no penalties on modern PC.

3. Public\private memory variables

4. Private variables can be changed outsid e?

5. Private variables can be changed outside?

6. Making Instace Variables Private/Local

7. Private instance variables

8. private variables

9. private variables (again)

10. private variable

11. cant see PRIVATE variable in MODULE SUBROUTINE when debugging with IFC/IDB v7.1

12. Accessing private variable as pointer

 

 
Powered by phpBB® Forum Software