0

When I put pygame.display.get_surface() as default param in func it gives me None, example:

def func(surface = pygame.display.get_surface()):
    print(surface)

And I get None.

But if I do this:

def func():
    print(pygame.display.get_surface())

It will print me the details of it (for example: Surface(800x600x32 SW))

What is the reason for it to happen?

Python
  • 127
  • 1
  • 14

1 Answers1

1
def func(surface=pygame.display.get_surface()):
    print(surface)

This calls pygame.display.get_surface() when the function is defined, not when it is called.

When the function itself is being created, you didn't initialize the pygame yet, so pygame.display.get_surface() returns None.

The alternative, as you noticed, is to move the get_surface() call from the definition of the function to the body:

def func(surface=None):
    if surface is None:
         surface = pygame.display.get_surface()
    print(surface)
nosklo
  • 217,122
  • 57
  • 293
  • 297