-3

I want to have an object of a class within a class, because I need to pass it to a method, similarly to the example below. I would like the example below to print out 1, or fooObj.fooNum, but I keep getting a NameError: name 'foo' is not defined.

class bar:
    def fooDef(self, fooObj):
        print fooObj.fooNum

class foo:
    fooNum = 1
    b = bar()
    f = foo()
    b.fooDef(f)
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Why are you placing that code inside the `foo` class definition at all? – user2357112 Aug 10 '18 at 16:36
  • It should work if you reference `foo` from `foo.__init__()`, but not as a Class variable. I think. – Alastair McCormack Aug 10 '18 at 16:38
  • 2
    This appears to be invalid class design: you have class-level code in `foo` that tries to create an object of its own class (without an initialization), pass the object to an *instance* method of `bar`, and then access a `foo` class attribute through the object. – Prune Aug 10 '18 at 16:39
  • @user2357112 In reality, this is for a much larger piece of code, one in which I must pass `self` or an instantiation of the class I am in. – Dhruv Jain Aug 10 '18 at 16:39
  • @AlastairMcCormack Could you please clarify? I didn't quite understand what you meant, does this mean that I have to make a `def __init__(self)` within the foo class, before I declare an object? – Dhruv Jain Aug 10 '18 at 16:42
  • @DhruvJain ignore me - it would create recursion. You probably need to rethink your design – Alastair McCormack Aug 10 '18 at 16:45
  • Related: [Create static instances of a class inside said class in Python](https://stackoverflow.com/q/2546608/2823755) – wwii Aug 10 '18 at 16:51

2 Answers2

1

Please, can you be more specific about what you are trying to do?

The error you see is normal, because the code immediately below class foo will be executed during the definition of foo and therefore the class is not defined yet.

If I understand well you want to define some method foobar of the class foo, which will use a foo instance. The correct procedure would then be

class foo:
    def foobar(self,):
        f = foo()
        ...

Again, with more details about what you are trying to do it would be easier to help you.

Alexis
  • 337
  • 1
  • 12
  • I want to create an instantiation of class A, and then pass it on to a method for class b, while I am in class A. – Dhruv Jain Aug 10 '18 at 16:47
  • @DhruvJain: Why do you want to do it "while I am in class A"? It sounds like you have some deep misunderstanding of what it means to execute code directly within the body of a class statement, but it's hard to tell what that misunderstanding might be. – user2357112 Aug 10 '18 at 16:53
  • Ok, then my answer does apply, just replace `...` with `b = bar()` ; `b.fooDef()`, The solution of @Maurice-Meyer works too and probably closer to your needs. The difference is that `__init__` is simply called at the instanciation of `foo` directly (so only once). – Alexis Aug 10 '18 at 16:57
1

Although it's unclear what you are asking, but the following changes do what you want to have.
But the code uses the instance of foo() not the class:

class bar:
    def fooDef(self, fooObj):
        print fooObj.fooNum

class foo:
    def __init__(self):
        self.fooNum = 1
        b = bar()
        f = self
        b.fooDef(f)

f = foo()

Prints:

1
Maurice Meyer
  • 17,279
  • 4
  • 30
  • 47