+ 2

Certain functions being assigned to variables

Why are some functions like so my_file = open(“my.file”,”r”) assigned to variables? I thought about this when looking at the Pygame module, as it has a certain function to create a surface that was put in an example like this: Win = pygame.display.set_mode((500,500)) I don’t know why I brought that up, but you hopefully get what I mean.

5th Jun 2018, 6:13 PM
...
... - avatar
6 Answers
+ 1
Because they return a value. Then you can assign the returned value to a variable-in other languages- of the same type.
5th Jun 2018, 6:16 PM
Bebida Roja
Bebida Roja - avatar
+ 1
so what would be the value of the first example? The contents of the file?
5th Jun 2018, 6:18 PM
...
... - avatar
+ 1
You should check what open() returns and what functionality is available to that object.
5th Jun 2018, 6:19 PM
Bebida Roja
Bebida Roja - avatar
+ 1
so it is the contents of the file
5th Jun 2018, 6:21 PM
...
... - avatar
+ 1
But it is not just an string. Its an object with some methods contained within, so that you can do things with it.
5th Jun 2018, 6:22 PM
Bebida Roja
Bebida Roja - avatar
+ 1
ohh ok, thank you!
5th Jun 2018, 6:23 PM
...
... - avatar