Beginner question? Classes, variables, ...

Discussion in 'Python' started by =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, Jun 28, 2006.

  1. 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 -
    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: Telf.: +34-985103687
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, Jun 28, 2006
    #1
    1. Advertising

  2. Ã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):
    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
    Diez B. Roggisch, Jun 28, 2006
    #2
    1. Advertising

  3. On Wed, 28 Jun 2006 10:35:10 +0200, "Diez B. Roggisch"
    <> declaimed the following in comp.lang.python:

    > class X:
    > def __init__(self, my_y):
    > self.my_y

    self.my_y = my_y

    --
    Wulfraed Dennis Lee Bieber KD6MOG

    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: )
    HTTP://www.bestiaria.com/
    Dennis Lee Bieber, Jun 28, 2006
    #3
  4. Dennis Lee Bieber wrote:

    > On Wed, 28 Jun 2006 10:35:10 +0200, "Diez B. Roggisch"
    > <> declaimed the following in comp.lang.python:
    >
    >> class X:
    >> def __init__(self, my_y):
    >> self.my_y

    > self.my_y = my_y


    *argl*

    Thanks :)

    No tea so far...

    Diez
    Diez B. Roggisch, Jun 28, 2006
    #4
  5. That wa sneat! Thanks!

    --
    Ãngel Gutiérrez Rodríguez -
    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: Telf.: +34-985103687
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, Jun 28, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=

    No Class at ALL!!! beginner/beginner question

    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=, Feb 2, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    572
    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=
    Feb 3, 2005
  2. lonelyplanet999
    Replies:
    1
    Views:
    2,212
    VisionSet
    Nov 13, 2003
  3. Razvan
    Replies:
    11
    Views:
    908
    Andrew Thompson
    Jul 17, 2004
  4. Replies:
    1
    Views:
    324
    Alf P. Steinbach
    Jul 24, 2007
  5. Replies:
    1
    Views:
    318
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=
    Jul 24, 2007
Loading...

Share This Page