Changing the private variables content

Gary Herron gherron at islandtraining.com
Tue Jul 21 17:14:44 EDT 2009


Ryniek90 wrote:
> Hi.
> I'm writing some class, and decided to use inside private method and 
> some private variables. While with method i haven't got any problem's 
> with variables i have.
> Maybe some example.
> A class with private method and private variable:
>
> "
> >>> class Secret(object):
>    #
>    def __init__(self):
>        self._number = 1
>    #
>    def _secret(self):
>        print self._number
>    def showit(self):
>        print "Secret number is:\n"
>        self._secret()
>
> >>> sec = Secret()
> >>> sec.showit()
> Secret number is:
>
> 1
> >>> sec._number
> 1
> >>> sec._number = 2
> >>> sec._number
> 2
> >>> sec._number += 3
> >>> sec._number
> 5
> >>>
> "
>
> As You can see, i made class with private method and private variable 
> inside __init__ constructor. I've changed also the variable value, but 
> outside class.
> I've got problem with changing some variable value _inside__my_ class, 
> which i'm writing.

Not sure this is what you are asking, but a method (which is how I 
interpret "_inside__my_ class") changes the value by normal assignment 
like this:

class Secret(object):
  ...
  def SetNumber(self,value):
    self._number = value

Is that what you were asking?


Gary Herron



> I've searched over google, some tuts with topics about operations on 
> private variables, but didn't found anything - only how to make them, 
> but no how to assign new objects/override them with new content (and 
> other advanced and helpful options).
>
> If someone could help me, it would be great.
>
> Thanks.




More information about the Python-list mailing list