Avoid double typing class names in python

Give your classes and modules meaningful names. That's the way. Name your module 'classes' and name your class 'MyClass'.

from package.classes import MyClass

myobj = MyClass()

You can use from ... import ... statement:

from package.obj import obj
my_obj = obj()

Python is not Java. Feel free to put many classes into one file and then name the file according to the category:

import mypackage.image

this_image = image.png(...)
that_image = image.jpeg(....)

If your classes are so large you want them in separate files to ease the maintenance burden, that's fine, but you should not then inflict extra pain on your users (or yourself, if you use your own package ;). Gather your public classes in the package's __init__ file (or a category file, such as image) to present a fairly flat namespace:

mypackage's __init__.py (or image.py):

from _jpeg import jpeg
from _png import png

mypackage's _jpeg.py:

class jpeg(...):
    ...

mypackage's _png.py:

class png(...):
    ...

user code:

# if gathered in __init__
import mypackage
this_image = mypackage.png(...)
that_image = mypackage.jpeg(...)

or:

# if gathered in image.py
from mypackage import image
this_image = image.png(...)
that_image = image.jpeg(....)

Tags:

Python