#include <iostream>
class B;
class A
{
public:
void Foo();
B *b;
};
class B
{
public:
void Bar();
A *a;
};
void A::Foo()
{
b->Bar();
}
void B::Foo()
{
std::cout << "A::Foo() B::Bar()\n";
}
int main()
{
A a;
B b;
a.b = &b;
b.a = &a;
a.Foo();
return 0;
}
how do this c++ code in python
Something like this ought to do it:
I'm no python expert, and I don't have python installed here at work, but it looks about right. You don't have to do any kind of foreward declaring of types in python. Python is typeless.
class A: b = None def foo(self): self.b.bar()class B: a = None def bar(self): print "things"a = A()b = B()a.b = bb.a = aa.foo()
I'm no python expert, and I don't have python installed here at work, but it looks about right. You don't have to do any kind of foreward declaring of types in python. Python is typeless.
thanks.
but is it creating a copy of a and b in A::a and B::b or is it referencing them? if the latter is happening then how can I create a copy of an object?
but is it creating a copy of a and b in A::a and B::b or is it referencing them? if the latter is happening then how can I create a copy of an object?
Quote:Original post by wendigo23Circular references don't work that easily in CPython. Objects a and b will never be released since they refer to each other. You gotta use the weakref module. Python is strongly and dynamically typed. Saying it is typeless can be confusing.
Something like this ought to do it:
*** Source Snippet Removed ***
I'm no python expert, and I don't have python installed here at work, but it looks about right. You don't have to do any kind of foreward declaring of types in python. Python is typeless.
Quote:but is it creating a copy of a and b in A::a and B::b or is it referencing them?It's referencing them, just like your C++ code is. If you want a copy, use the copy module and implement __copy__ or __deepcopy__ in your classes if desired.
so how could I for instance create a copy of B and also reference it?
also, wouldn't a and b be destroyed when they're both destroyed?
also, wouldn't a and b be destroyed when they're both destroyed?
Quote:Original post by GenjixJust take a look at the copy module documentation..
so how could I for instance create a copy of B and also reference it?
Quote:also, wouldn't a and b be destroyed when they're both destroyed?That's what one would like to happen, but alas it won't. After a = A(); b = B() the reference counts are 1 for both objects. After a.b = b; b.a = a the reference counts are 2 for both. When a and b variables go out of scope (function exit), reference counts decrease by 1, leaving 1 still and neither will be destroyed.
I was under the impression that recent versions of python were able to handle circular references.
Quote:Original post by wendigo23Well yes, another option would be to enable the garbage collector, since it isn't on by default. But it won't collect cycles in which some of the objects have __del__ methods.
I was under the impression that recent versions of python were able to handle circular references.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement