use of __new__ to permit "dynamic" completion within (any?) IDE ?
gst
g.starck at gmail.com
Tue Dec 7 10:52:06 EST 2010
Hi,
I met a situation where I was passing an object created in/with an
upper level module class to a lower level module class' instance in
one of its __init__ argument and saving a ref of the upper object in
that lower level class' new instance.
But in my IDE I want the completion to also work from within the lower
level module when it's refering to the object passed from the upper
level:
Well, I'm sure I'm not very clear in my terms (and probably a bit long
in the sentence) so here it is in code:
files:
module1.py
subpackage/module2.py
file module1.py:
from subpackage.module2 import class2
class class1(object):
def __new__(cls, _self=None, *args, **kwargs):
if _self: ## we've been passed an instance already
initialyzed
## so directly return it instead of creating a
new object.
return _self
return object.__new__(cls)
def __init__(self, _self=None, *args, **kwargs):
if _self: ## we've been passed an instance already
initialyzed
## so directly returns
## assert(self is _self) ?
return
self.object2 = class2(object1=self, "blip", "blop")
# others init
file module2.py:
class class2(object):
def __init__(self, object1, *args, **kwargs):
from ..module1 import class1
self.object1 = class1(_self=object1) ## instead of:
self.object1 = object1
## others functions and/or init..
## where now I've completion working on self.object1 :
## if I add(or remove) fields/methods in module1 (and save) then
## I have them available(or disappeared) in the completion when
executed from this submodule.
## This ofcourse permits to save me of remembering all of the
class1 attributes/methods when I'm working with self.object1 from
within one of class2 methods.
What do you think of this ?
I guess there can be others ways of doing this.. ?
Thanks,
Regards,
Greg.
note: I'm using Eclipse 3.5.2 with pydev so (I don't really know how
others IDE can handle this case) .
More information about the Python-list
mailing list