two newbie questions "cyclic class dependencies" and "date from string"
Daan Hoogland
hoogland at astron.nl
Tue May 13 02:30:10 EDT 2003
H everyone,
I'm new as the title sugests. And I'd like two classes to be able to
instantiate each other:
<file name='both.py'>
class A:
def __init__(self):
self.b = B();
class B:
def delegate(self, a):
a = A();
return a;
x = A();
y = None;
y = x.b.delegate(y);
print x;
print y;
</file>
this works. Now I want to have these lines in three files. so adding some
import statements seemed the obvious. It failed for equally obvious reasons:
ImportError: cannot import name A
<file name="A.py">
from B import B;
class A:
def __init__(self):
self.b = B();
</file>
<file name="B.py">
from A import A;
class B:
def delegate(self, a):
a = A();
</file>
<file name="C.py">
from A import A;
x = A();
y = None;
y = x.b.delegate(y);
print x;
print y;
</file>
<stderr>
bash-2.04$ python C.py
Traceback (most recent call last):
File "C.py", line 1, in ?
from A import A;
File "/data/home/daan/tmp/A.py", line 1, in ?
from B import B;
File "/data/home/daan/tmp/B.py", line 1, in ?
from A import A;
ImportError: cannot import name A
</stderr>
Is there a trick around this? Any advice on reading on object orientation in
python? My oo is good, i think.
I can't find a function to convert a date string back to a datetime object. The
date i have is returned by postgresql in the (well known) format "YYYY-MM-DD
hh:mm:ss:mmmmmm". Am i missing something in the help? Or is there an extended
Date package that does this?
thanks,
More information about the Python-list
mailing list