Create an object of the same class within the class

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Create an object of the same class within the class



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.


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)





Why are you placing that code inside the foo class definition at all?
– user2357112
Aug 10 at 16:36


foo





It should work if you reference foo from foo.__init__(), but not as a Class variable. I think.
– Alastair McCormack
Aug 10 at 16:38



foo


foo.__init__()





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 at 16:39


foo


bar


foo





@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 at 16:39


self





@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 at 16:42


def __init__(self)




2 Answers
2



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.


class foo


foo



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


foobar


foo


foo


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.





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 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 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 at 16:57


...


b = bar()


b.fooDef()


__init__


foo



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:


foo()


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





Thanks so much! this is exactly what I wanted to do!
– Dhruv Jain
Aug 10 at 16:49






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS