Hi there!

I'm wondering if there is a "package" instruction in python so I can in a directory keep packaging all the classes and I don't have to import them every time. I mean, if in a same directory I have a class DutiesManager that controls an array of instances of the class Dutie, I don't want to do in the DutiesManager an "import Dutie", just "package foo.foo1" and I can use in DutiesManager all the classes of that package.

I don't know if I have explained that good, sorry for my English.

Thank you!!!

Recommended Answers

All 4 Replies

You can create and save a module of your favorite class collection this way:

# put number of your favorite classes into one module
# save it as MyClassCollection.py (preserve filename case) somewhere
# on the PYTHONPATH so Python's import can find it

class ClassA(object):
    def __init__(self, color='blue'):
        self.color = color
    def show(self):
        print "ClassA likes color", self.color

class ClassB(object):
    def __init__(self, color='red'):
        self.color = color
    def show(self):
        print "ClassB likes color", self.color

class ClassC(object):
    def __init__(self, color='green'):
        self.color = color
    def show(self):
        print "ClassC likes color", self.color

# optionally test this module ...
if __name__ == '__main__':
    a = ClassA()
    b = ClassB()
    c = ClassC('yellow')
    a.show()
    b.show()
    c.show()

"""
result -->
ClassA likes color blue
ClassB likes color red
ClassC likes color yellow
"""

Once you have saved the collection you can access it this way:

# MyClassCollection.py is collection of your favorite classes
# it should be somewhere on the PYTHONPATH so Python can find it
# this program shows how to access this collection
# note that filename and modulename are case sensiive!

import MyClassCollection as mcc

# to make typing easier the optional alias 'mcc' is used for 'MyClassCollection'
# mcc becomes the namespace to access the class collection

# test it ...
a = mcc.ClassA()
b = mcc.ClassB()
c = mcc.ClassC('yellow')
a.show()
b.show()
c.show()

"""
result -->
ClassA likes color blue
ClassB likes color red
ClassC likes color yellow
"""

I hope that's what you where looking for? BTW, function collections would be handled the same way.

Yes more or less that was the idea I was looking for, but I think the way you have taught me isn't the way OOP works (at least what I've learn, not too much) .The idea is have each class in a file , and in a directory (a package containing all the files (classes)) a bunch of classes ready to use.

The question was referenced to the visibility field. I mean, imagine you have 3 files. "ClassA.py", "ClassB.py","ClassC.py" in a directory called "foo".

(in java):
If the three classes have the first line of code "package foo;", inside, for example ClassB, I can do with no problem ClassA caobj = new ClassA() and ClassC cbobj = new ClassC() without any "import ClassA" or "Import ClassC".

In python I don't know how it works, but my experience says that in the same example, three classes in a directory, inside class B I would need to do an import ClassA and an import ClassC in order to work with instances of that classes.

To sum up, I was just wondering if there is something similar to the instruction "package" in java in order to keep all the classes in different files and package them with the "visibility package" surrounding all the classes of a package (non-import statements).


Thank you very much for answering!!!

Yes, Python allows you to create packages in one folder, that's how wx works. I just don't know how to do this yet!

It's acceptable in OOP to have multiple, related classes in one file. What you want to avoid is a garbage-dump file that has unrelated classes in it.

You might take a look at the source code in the Lib directory. audiodev.py, for example, contains multiple classes along with other functions.

(This doesn't answer your question about packages; I don't know that one -- sorry!)

Jeff

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.