Beginner question? Classes, variables, ...

G

Guest

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

Thanks in advance.
--
Ãngel Gutiérrez Rodríguez - (e-mail address removed)
Instituto de Ciencia de los Materiales de Madrid - CSIC
SpLine - European Syncrothorn Radiation Facility - Grenoble - France

Postal adress: Departamento de Química Física y Analítica
Universidad de Oviedo - c/Julián Clavería 8 33006 - Oviedo
Asturias - Spain
E-mail: (e-mail address removed) Telf.: +34-985103687
 
D

Diez B. Roggisch

Ãngel Gutiérrez Rodríguez said:
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):
self.my_y

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

class Y:
def __init__(self):
self.a=1
self.b=X(self)

Then in X you can work with whatever Y contains.



Diez
 
G

Guest

That wa sneat! Thanks!

--
Ãngel Gutiérrez Rodríguez - (e-mail address removed)
Instituto de Ciencia de los Materiales de Madrid - CSIC
SpLine - European Syncrothorn Radiation Facility - Grenoble - France

Postal adress: Departamento de Química Física y Analítica
Universidad de Oviedo - c/Julián Clavería 8 33006 - Oviedo
Asturias - Spain
E-mail: (e-mail address removed) Telf.: +34-985103687
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,835
Latest member
KetoRushACVBuy

Latest Threads

Top