Beginner question? Classes, variables, ...

Diez B. Roggisch deets at
Wed Jun 28 10:35:10 CEST 2006

Ángel Gutiérrez Rodríguez wrote:

> The problem:
> I have two classes:
> class X:
>   def __init__(self):
>     pass
> class Y:
>   def __init__(self):
>     self.a=1
>     self.b=X()
> and I would like to make 'a' visible inside 'x'. Is there a way to refer
> to the Y class from the X? To make things easier :), each class is in a
> different file, so class X is imported. Or the only way I have is to
> pass 'a' as a variable in each method call of 'b' ('a' can take different
> values that affect to the behaviour of 'b').

You mean the behavior of X here I guess - b is just a name, there isn't much
behavior in it.

Pass X the instance of Y:

class X:
  def __init__(self, my_y):

  def foo(self):
      print self.my_y.a

class Y:
  def __init__(self):

Then in X you can work with whatever Y contains.



More information about the Python-list mailing list