A situation when to use star imports

This is the place for queries that don't fit in any of the other categories.

A situation when to use star imports

Postby metulburr » Thu Jan 30, 2014 12:56 pm

I was pretty much looking at whether or not my thinking was corrent or not?

This is the only time i can think of when star imports would be useful.
https://github.com/metulburr/pong/blob/ ... trol.py#L4

This is just a pong game. If i was to add a lot more features, or whatever, this line would get rediculously long. OR is there another alternative to star imports even in this situation?
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1476
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: A situation when to use star imports

Postby Yoriz » Thu Jan 30, 2014 1:06 pm

Original code
Code: Select all
from .states import classic, menu, mode, options, controls, audio, ghost, splash, keybinding, getkey

wrap it using brackets
Code: Select all
from .states import (classic, menu, mode, options, controls, audio, ghost,
                     splash, keybinding, getkey)
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 871
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: A situation when to use star imports

Postby Mekire » Thu Jan 30, 2014 1:07 pm

Massive edit here:

OK so you putting a * import within the __init__ doesn't work apparently so it is a moot point.
But if you want to clean things a bit, you can import all the individual modules within the __init__ and then import the package directly.

Within states.__init__
Code: Select all
from . import (classic, menu, mode, options, controls,
               audio, ghost, splash, keybinding, getkey)


Then your import in your actual module becomes:
Code: Select all
from . import states
or if you aren't inside a package:
Code: Select all
import states

Then you qualify your states as normal:
Code: Select all
states.classic
states.menu
#etc.


Ok I think my retardation is fixed... not sure.
-Mek
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: A situation when to use star imports

Postby stranac » Thu Jan 30, 2014 1:25 pm

Just import the states module and use that.
Also, don't use relative imports, they suck.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1152
Joined: Thu Feb 07, 2013 3:42 pm

Re: A situation when to use star imports

Postby Mekire » Thu Jan 30, 2014 1:31 pm

stranac wrote:Just import the states module and use that.
Also, don't use relative imports, they suck.

Here states isn't a module; it is a package (within a package). If you just import states you won't be able to find the modules.

-Mek
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: A situation when to use star imports

Postby stranac » Thu Jan 30, 2014 2:37 pm

I see. Should have actually taken a look at the repo...

In this case, I would probably do something like Yoriz and Mekire are suggesting.
But with an absolute import.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1152
Joined: Thu Feb 07, 2013 3:42 pm

Re: A situation when to use star imports

Postby metulburr » Fri Jan 31, 2014 10:22 am

hmm, That seems like the simpler explicit method is what it currently is then.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1476
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY


Return to General Coding Help

Who is online

Users browsing this forum: buran, Google [Bot] and 3 guests