开发者

When instancing an imported "class" from a package, how can i avoid using the package.class() declaration?

I'm kinda new to Python, so i'm still lost in the whole namespace thing. I've created a package, with the init file in it and also a classname.py file, with the class, obviously. For instance: from parachute import container, emitter

I tried to instance the class Container directly, but it gave me an error, so i had to instance it as container.Container(). How can i avoid doing this? Basically, what i want to do is to import a class from a package and avoid typing the package name and/or the file name. Thanks in advance, and please let me know if the question isn't clear enough.

UPDATE The structure i have is: - pa开发者_Go百科rachute -- init.py

-- container.py
    Serves as a controller, i'd say, instancing, calling and glueing all the other parts    together.

-- sparkles.py
    Has two classes: Sparkle and Sparkles. Sparkle is a single element, with only one    property so far, and Sparkles serves as a collection.  Sparkles() belongs to the Emitter, and Sparkle() belongs to Sparkles().

-- emitter.py 
    Emitter could be seen as the user entity. It has a name and an uid, it belongs to a Container and the Container belongs to it.

Now, from outside the package i'm calling Container and passing some arguments, and the Container instances and distributes the arguments as it needs. I have the impression that this isn't the best way to do what i need to do, which is: Create a collection of sparkles, owned by the emitter.


Don't put the class in it's own file. Put Container and Emitter directly in parachute.py.

You can then do

from parachute import Container, Emitter

or

import parachute

container = parachute.Container()

This essentially boils down to "Python isn't Java so for best results, don't treat it like it is" ;)


from module import Class
classInst = Class()

This will work if your class is in module.py

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜