Two objects are created, the first and the second. They are created once. We need to make them get links to each other:

a = func(b) b = func(а) 

Understandably, it returns an error that b not defined. I know there is a clever solution to this problem, once I saw it. Maybe, you know? If you tell in Python, it will be generally excellent.

  • What are you trying to do? - Xander
  • More specifically, two objects are created. The first and second. They are created once. We must do so that they get links to each other - Artem Surov
  • Aside: avoid circular links if there is no good reason to use them. - jfs

2 answers 2

You must first create both objects, and then create an attribute in each of the objects, which will refer to the second object.

Then each of the objects can access this attribute when it needs a reference to the "partner".

 class func(object): def check(self): print self.link a = func() b = func() a.link = b b.link = a print(a.check()) print(b) # Выведет: # <__main__.func object at 0x7fc9c3d3a050> # <__main__.func object at 0x7fc9c3d3a050> print(b.check()) print(a) # Выведет # <__main__.func object at 0x7fc9c3d29fd0> # <__main__.func object at 0x7fc9c3d29fd0> 

As you can see, each of the objects really now has access to the second object.

And then you can do it like this :)

 print(a.link.link.link.link.link.link.link.link.link) # Выведет # <__main__.func object at 0x7fc9c3d3a050> 
  • Yes, thank you very much, everything can really be much easier and work this way) - Artem Surov

There is a very tricky way to initialize variables before using them:

 a = 0; b = 0; a = func(b); b = func(а); 

Profit!

  • unfortunately, I tried it this way, and it does not work that way, syntactically, of course, as an option, but I just need functions. pass on. the very beginning. But anyway, thanks for not having passed by.) - Artem Surov
  • @ ArtyomSurov is not, you can’t do that, and you don’t need to. you have an error somewhere in logic - strangeqargo
  • @ArtemSurov, please explain what exactly you need to do, otherwise it is difficult to help you. - Aim X
  • @strangeqargo Yes, you can, somehow do it. In python at least, but only with the help of a very clever crutch .. - Artem Surov