Beginner question? Classes, variables, ...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ángel Gutiérrez Rodríguez

    #1

    Beginner question? Classes, variables, ...

    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 - agr@fq.uniovi.e s
    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: agr@fq.uniovi.e s Telf.: +34-985103687
  • Diez B. Roggisch

    #2
    Re: Beginner question? Classes, variables, ...

    Ángel Gutiérrez Rodríguez wrote:
    [color=blue]
    > 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').[/color]

    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

    Comment

    • Diez B. Roggisch

      #3
      Re: Beginner question? Classes, variables, ...

      Dennis Lee Bieber wrote:
      [color=blue]
      > On Wed, 28 Jun 2006 10:35:10 +0200, "Diez B. Roggisch"
      > <deets@nospam.w eb.de> declaimed the following in comp.lang.pytho n:
      >[color=green]
      >> class X:
      >> def __init__(self, my_y):
      >> self.my_y[/color]
      > self.my_y = my_y[/color]

      *argl*

      Thanks :)

      No tea so far...

      Diez

      Comment

      • Ángel Gutiérrez Rodríguez

        #4
        Re: Beginner question? Classes, variables, ...

        That wa sneat! Thanks!

        --
        Ángel Gutiérrez Rodríguez - agr@fq.uniovi.e s
        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: agr@fq.uniovi.e s Telf.: +34-985103687

        Comment

        Working...