Referencing module.instance
Ben Finney
ben+python at benfinney.id.au
Fri Dec 2 16:31:18 EST 2011
Gnarlodious <gnarlodious at gmail.com> writes:
> What I am doing is importing modules that have an identical instance
> name.
Best to fix that, then.
> import Grid
That's a poorly-named module. PEP 8 recommends module names be all
lowercase.
> Grid has its instance:
>
> Grid.Grid()
And this is the reason: PEP 8 recommends class names be named with
TitleCase, so they're distinguishable from module names.
So the above should be:
import grid
grid.Grid()
> for page in self.allowedPages:
> setattr(self, page, __import__(page))
For completeness, I'll note that the function name would be PEP 8
compatible as ‘allowed_pages’.
> The problem is that the attribute name needs to reference the
> Grid.Grid instance and not the Grid module. How would I do this?
> I can do it literally:
> setattr(self, 'Grid', Grid.Grid)
Why do you shy away from this? You're already using ‘__import__’, and I
don't know why that is since you already showed that you're importing
the module explicitly anyway.
--
\ “Don't be afraid of missing opportunities. Behind every failure |
`\ is an opportunity somebody wishes they had missed.” —Jane |
_o__) Wagner, via Lily Tomlin |
Ben Finney
More information about the Python-list
mailing list