can a method access/set another's variables?

wswilson wswilson at gmail.com
Mon Apr 2 18:51:54 CEST 2007


On Apr 2, 11:01 am, Dennis Lee Bieber <wlfr... at ix.netcom.com> wrote:
> On 1 Apr 2007 18:36:04 -0700, "asdf1234234" <wswil... at gmail.com>
> declaimed the following in comp.lang.python:
>
> > My code is:
> > -a.py-
> > import b
>
> > class A:
> >      def __init__(self):
> >           pass
>
>         Delete this empty __init__() [and, consider converting the class to
> new-style]
>
> >      def my_method(self):
> >           var = 1
>
>         var is a local to method only name, it goes away when the method
> ends.
>
> >           self.var = 2
> >           b.set_var(self)
> >           print var
> >           print self.var
>
> > my_a = A()
> > my_a.my_method()
>
> > -b.py-
> > def set_var(self):
> >      var = 2
>
>         Again, var is local to just set_var().
>
> >      self.var = 2
>
> > I want both var and self.var to be 2 at the end. Is there anything I
> > can pass to set_var() that will give it access to the variables in
> > my_method() like I can use self for the variables in the class A?
>
>         The common scheme, unless you expect to have multiple instances of
> this class with /separate/ local "var", would be to put var in a
> "common" module
>
> -=-=-=-=- common.py
>
> var = None
>
> -=-=-=-=-
>
>         Then have both a.py and b.py "import common", followed by changing
> all "var =" to "common.var ="
> --
>         Wulfraed        Dennis Lee Bieber               KD6MOG
>         wlfr... at ix.netcom.com             wulfr... at bestiaria.com
>                 HTTP://wlfraed.home.netcom.com/
>         (Bestiaria Support Staff:               web-a... at bestiaria.com)
>                 HTTP://www.bestiaria.com/

Thanks! I like the sound of that...it should work for what I need it
to do.




More information about the Python-list mailing list