I have a problem understanding some description of functions in Python.
I understand simply functions like os.putenv(varname, value)
but I have no idea how to use this: os.getenv(varname[, value])
. How to pass arguments to that function, what does those square brackets mean?

- 115,751
- 26
- 228
- 437

- 27
- 7
-
it means that the parameter between brackets may be omitted. – Jean-François Fabre Oct 29 '16 at 20:55
2 Answers
Square brackets typically mean that the value is optional. Here, varname
refers to the environment variable you want to get and value
is an optional value that is return if the environment variable doesn't exist.

- 891
- 7
- 17
The square brackets indicate the argument is optional. Read the description:
Return the value of the environment variable varname if it exists, or value if it doesn’t. value defaults to
None
.
So you can call it like this:
os.getenv('NONEXISTANTVAR')
or like this:
os.getenv('NONEXISTANTVAR', u'my default')
This first is equivalent to os.getenv('NONEXISTANTVAR', None)
. In the specific case of getenv
, the second argument is returned if the environment variable doesn't exist.
Typically, the documentation will tell you the default value if you don't provide one, either explicitly in the description or by placing an =somevalue
directly in the signature. If it doesn't indicate a specific value, then it will at least describe the difference in behavior.
unicode
gives us an example of the =
in the signature:
unicode
(object='')
Note that in Python, the only way for an argument to be optional is for it to have a default value.

- 28,463
- 14
- 94
- 146