variable access question

Robert Cragie rcc at nospamthanks_jennic.com
Thu May 11 12:40:26 EDT 2000


A possible enhancement could be to pass the an instance of A as a
constructor argument. This gives a bit more flexibility:

class A :
  def __init__(self) :
    # Make queue an instance member.
    self.queue = Queue(10)

# Create N A instances.
a1 = A()
a2 = A()

class B :
  def __init__(self, a) :
    # Make A instance an instance member.
    self.a = a

def method(self) :
  # Access to the queue member of the A instance passed on construction
  print self.a.queue

# Create N B instances, referencing different instances of A
b1 = B(a1)
b2 = B(a1)
b3 = B(a2)

# Call bs methods.
b1.method()

Laurent POINTAL <pointal at lure.u-psud.fr> wrote in message
news:391a5dfe.3261520348 at news.u-psud.fr...
| On Wed, 10 May 2000 12:18:18 -0400, oliver <fwang2 at yahoo.com> wrote:
|
| >:
| >: thanks for reply, and yes, I meant to have mulitple instance of class
B,
| >: and they all want to acceess one instance of Class A's "queue"
variable,
| >: the code follows doesn't seem to be able to do it. I am a bit confused
| >: here. could you clarify it on that?
| class A :
| def __init__(self) :
| # Make queue an instance member.
| self.queue = Queue(10)
|
| # Create an A instance.
| a = A()
|
| class B :
| def method(self) :
| # Access to the queue member of the global a instance.
| print a.queue
|
| # Create N B instances.
| b1 = B()
| b2 = B()
| b3 = B()
| ...
|
| # Call bs methods.
| b1.method()
|
| & Co.
|
| A+
|
| Laurent.
|
| ---
| Laurent POINTAL - CNRS/LURE - Service Informatique Experiences
| Tel/fax: 01 64 46 82 80 / 01 64 46 41 48
| email  : pointal at lure.u-psud.fr  ou  lpointal at planete.net





More information about the Python-list mailing list