This is closely related to: Importing modules in Python - best practice , but the following case is not really discussed in details.
If we have a module coordinates
that defines one class Coordinates
and several utility function to work with the Coordinates
type. What is the recommended way to do the following two things:
coordinates
moduleCoordinates
to coordinates.Coordinates
The two options I see for now:
import coordinates
from coordinates import Coordinates
But that seems a bit odd, on the other hand the other solution I see doesn't seem very clean either:
import coordinates
Coordinates = coordinates.Coordinates
Which one of this two is the most commonly used or the prefered way of doing this? Or maybe both of these shouldn't be used, in which case what would be a better solution?
Another option I'm considering but that I would prefer to avoid is one proposed in the referenced question: import coordinates as crd
and then simply use crd.Coordinates
instead of Coordinates
. The reason why I want to avoid this is because it would make my code less readable, in particular when I use a function from the coordinates
module. To this alternative I would probably prefer to simply call coordinates.Coordinates
even if that looks redundant.
I've also considered making my module callable to make coordinates()
to automatically call Coordinates()
but I'm not really sure it's really a good solution either (and I'm not sure how to handle the documentation in such case).
It is a matter of personal preference, but for myself, I'd go with:
from coordinates import Coordinates
Unless , that is, you are using more than, oh, three or four things from the coordinates
module, or one of the names you are importing from it clashs with names you are using from elsewhere. In either of those cases, go with
import coordinates
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.