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?
we will be moving to python-forum.io on October 1 2016
more details here
User avatar
metulburr
 
Posts: 2243
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)
Due to the reasons discussed here we will be moving to python-forum.io/ on October 1 2016
This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.
User avatar
Yoriz
 
Posts: 1672
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
New Users, Read This
  • Use code tags when posting code.
  • Include any errors with your post (in code tags).
  • Describe your problem; not your chosen solution.
  • Make examples the minimum length to demonstrate your issue.
User avatar
Mekire
 
Posts: 1710
Joined: Thu Feb 07, 2013 11:33 pm
Location: Tucson, Arizona

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: 1790
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
New Users, Read This
  • Use code tags when posting code.
  • Include any errors with your post (in code tags).
  • Describe your problem; not your chosen solution.
  • Make examples the minimum length to demonstrate your issue.
User avatar
Mekire
 
Posts: 1710
Joined: Thu Feb 07, 2013 11:33 pm
Location: Tucson, Arizona

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: 1790
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.
we will be moving to python-forum.io on October 1 2016
more details here
User avatar
metulburr
 
Posts: 2243
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot] and 2 guests