26

I'm learning python via book and internet and I'm stuck on a class issue.

2 questions:

  1. How do I create an instance of one class in another (separate) class?
  2. How do I pass variables between the class and the nested(?) class?

When I try to create an instance of a class within another (separate) class, i'm able to do so within a method. Here's the code:

import os

class FOO():
    def __init__(self):
        self.values = [2, 4, 6, 8]

    def do_something(self, x, y):
        os.system("clear")
        z = self.values[x] * self.values[y]
        print "%r * %r = %r" % (self.values[x], self.values[y], z)


class BAR():
    def __init__(self):
        pass

    def something_else(self, a, b):
        foo1 = FOO()
        foo1.do_something(a, b)

bar = BAR()
bar.something_else(1, 2)

Will this, however, allow me to access the FOO class and it's information in other BAR methods? If so, how?

I tried the following and got an error:

import os

class FOO():
    def __init__(self):
        self.values = [2, 4, 6, 8]

    def do_something(self, x, y):
        os.system("clear")
        z = self.values[x] * self.values[y]
        print "%r * %r = %r" % (self.values[x], self.values[y], z)


class BAR():
    def __init__(self):
        foo1 = FOO()

    def something_else(self, a, b):
        foo1.do_something(a, b)

bar = BAR()
bar.something_else(1, 2)

Here is the error:

Traceback (most recent call last):
File "cwic.py", line 22, in <module>
bar.something_else(1, 2)
File "cwic.py", line 19, in something_else
foo1.do_something(a, b)
NameError: global name 'foo1' is not defined

I get the same error when I used:

def __init__(self):
    self.foo1 = FOO()

Also, how should I pass the 'values' from one class to the other?

Please let me know if my general approach and/or syntax are wrong.

martineau
  • 119,623
  • 25
  • 170
  • 301
DBWeinstein
  • 8,605
  • 31
  • 73
  • 118

5 Answers5

12

All attributes of an instance or class are accessed via self which is passed as the first argument to all methods. That's why you've correctly got the method signature something_else(self, a, b) as opposed to just something_else(a, b) as you might with other languages. So you're looking for:

    class BAR():
        def __init__(self):
            self.foo1 = FOO()

    def something_else(self, a, b):
        self.foo1.do_something(a, b)

Note that self isn't a keyword, it's just a paramater name like a and b, so you might for example be tempted to use this instead if you're from a Java background... Don't though! there is a very strong convention to use self for this purpose and you will make a lot of people (including yourself eventually) very angry if you use anything else!

With regards to passing values I'm not sure what you mean, you can access them through instances such as foo1.an_attribute or pass them as arguments to functions as in function(arg) but you seem to have used both techniques in your example so I'm not sure what else you're after...

Edit

In response to @dwstein's comment, I'm not sure how it's done in VB but here's a how you pass arguments when creating an instance of a class.

If we look at your class:

class BAR():
    def __init__(self):
        self.foo1 = FOO()

We can change that to accept an argument baz by changing second line to def __init__(self, baz): and then if we want we can make baz an attribute of our new instance of BAR by setting self.baz = baz. Hope that helps.

redrah
  • 1,204
  • 11
  • 20
  • Thanks for your thorough answer! Very helpful. I'm trying to understand how pas information from one class to another. I guess I've seen arguments as part of class definitions and I'm wondering if that's the right way. But, I don't know how to use that functionality. by the way, i've got a VBA background. – DBWeinstein Aug 17 '12 at 16:23
2

In python you always have to use the "self" prefix when accessing members of your class instance. Try

class BAR():
    def __init__(self):
        self.foo1 = FOO()

    def something_else(self, a, b):
        self.foo1.do_something(a, b)
Zvi
  • 237
  • 3
  • 8
2

You're on the right track, try:

class BAR():
    def __init__(self):
        self.foo1 = FOO()

    def something_else(self, a, b):
        self.foo1.do_something(a, b)
Bi Rico
  • 25,283
  • 3
  • 52
  • 75
0

I'm not sure if I understand your question, which is more of a reflection of me than of you. If I understand what you are looking for, you want to create an instance of a class within a class method. Now, I could be 'way off-base here, so if this is confusing, it may be that I am answering a different question from your question.

#! /usr/bin/env python3

class MyClass ( object ):

  instance_list = []  

  def __init__ ( self, arg1, arg2):
    self.arg1 = arg1
    self.arg2 = arg2

  @classmethod
  def make_instances( cls ):
    for a1 in range(1,4):
      for a2 in range(1,3):
        cls.instance_list.append( MyClass( a1, a2 ) )

  @classmethod
  def dump_instance_list ( cls ):
    for instance in cls.instance_list:
      print( "arg1= %d\targ2= %d" % ( instance.arg1, instance.arg2) )

if __name__ == "__main__" :
  MyClass.make_instances()
  MyClass.dump_instance_list()
  an_instance = MyClass(14, 22)
  print("An instance: %d, %d" % (an_instance.arg1, an_instance.arg2))

What this program does is create a class, MyClass, which has a class object, instance_list. instance_list is going to be a list of instances. class method make_instances does just that: it creates instances and populates instance_list. At the end of the program, I create an_instance the way one would "normally" create an instance. So an_instance is an object of class MyClass.

I hope this is helpful

Jeff Silverman
  • 692
  • 1
  • 8
  • 15
0

If you don't create a def __init__(self): method for a class, you have to initialize the class as: eg:

class XYZ:
    def foo(self):
        ....
        ....
r1 = XYZ()

and to access the method you have to write r1.foo() But as you have created a init method you have to access the foo function as self.foo() which is equal to r1.foo() so in your code, instead of foo1.do_something(), it should be self.foo1.do_something()

Saeed Zhiany
  • 2,051
  • 9
  • 30
  • 41