I can’t understand that kind of import

Asked

Viewed 44 times

0

I’m beginner in Python and I came across something I didn’t understand:

import pygame

from pygame.sprite import Sprite

    class Bullet(Sprite):

        def __init__(self,ai_settings,screen,ship):

My question is what would it be pygame.sprite import Sprite?

  • from pygame.sprite import Sprite means: From the pygame.Prite module import the Sprite class. <(-_-)>

  • Sprite is a submodule ?

  • What part of From the pygame.Prite module import the Sprite class. you don’t understand?

  • is the pygame.Sprite

  • You have the pygame lib, in addition to the main lib it divided into several other modules, one for each subject. So it can be understood as a specialized submodule, but in reality it’s built-in or nested modules.

1 answer

2

The first import will load the module pygame within the scope (namespace) of your file/module while the second will only load the class "Sprite" in it. Actually the second import is there as a shortcut since nothing stops you from writing:

import pygame

class Bullet(pygame.sprite.Sprite):
    ...

Perhaps only practicality since "Sprite" is much smaller than "pygame.sprite.Sprite". :-)

For from the point of view of language they correspond to the same object:

>>> import pygame
pygame 2.0.1 (SDL 2.0.14, Python 3.8.6)
Hello from the pygame community. https://www.pygame.org/contribute.html
t>>> id(pygame.sprite.Sprite)
43004016
>>> from pygame.sprite import Sprite
>>> id(Sprite)
43004016

Browser other questions tagged

You are not signed in. Login or sign up in order to post.