Sign in to follow this  
Genjix

how do this c++ code in python

Recommended Posts

Genjix    100
#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;
}

Share this post


Link to post
Share on other sites
wendigo23    512
Something like this ought to do it:

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 = b
b.a = a

a.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.

Share this post


Link to post
Share on other sites
Genjix    100
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?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Quote:
Original post by wendigo23
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.
Circular 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.
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.

Share this post


Link to post
Share on other sites
Genjix    100
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?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Quote:
Original post by Genjix
so how could I for instance create a copy of B and also reference it?
Just take a look at the copy module documentation..
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Quote:
Original post by wendigo23
I was under the impression that recent versions of python were able to handle circular references.
Well 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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this