1

I am looking at a snippet of code and I just don't understand how it works:

import pygame, sys
from pygame.locals import *

on the first line pygame is imported, and on the second line, all the methods of a subset of pygame is invoked. If the first line imports all of pygame, why do we have to specifically import a subset of the module again? Why doesn't a mere import pygame do the job in the first place?

Selcuk
60.1k12 gold badges114 silver badges119 bronze badges
asked Mar 17, 2015 at 19:02
1

2 Answers 2

4

A mere import pygame would suffice, but the author wanted to have a shorthand access to the constants of pygame. For example, instead of:

import pygame
...
resolution = pygame.locals.TIMER_RESOLUTION 

it may be sometimes preferable to have

import pygame
from pygame.locals import *
...
resolution = TIMER_RESOLUTION 

Note that you should still import pygame itself to be able to access to other methods/properties (other than pygame.locals.) of pygame.

answered Mar 17, 2015 at 19:07
Sign up to request clarification or add additional context in comments.

Comments

2

The idea is that you can call all the functions in pygame.locals without using pygame.locals.someFunction, but instead someFunction.

answered Mar 17, 2015 at 19:05

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.