In the exact scenario you present, you can use default values for arguments, as other answers show.
Generically, you can use the or keyword in Python pretty similarly to the way you use || in JavaScript; if someone passes a falsey value (such as a null string or None) you can replace it with a default value like this:
string = string or "defaultValue"
This can be useful when your value comes from a file or user input:
string = raw_input("Proceed? [Yn] ")[:1].upper() or "Y"
Or when you want to use an empty container for a default value, which is problematic in regular Python (see this SO question):
def calc(startval, sequence=None):
sequence = sequence or []
Answer from kindall on Stack OverflowIn the exact scenario you present, you can use default values for arguments, as other answers show.
Generically, you can use the or keyword in Python pretty similarly to the way you use || in JavaScript; if someone passes a falsey value (such as a null string or None) you can replace it with a default value like this:
string = string or "defaultValue"
This can be useful when your value comes from a file or user input:
string = raw_input("Proceed? [Yn] ")[:1].upper() or "Y"
Or when you want to use an empty container for a default value, which is problematic in regular Python (see this SO question):
def calc(startval, sequence=None):
sequence = sequence or []
For a function parameter, you can use a default argument value:
def test(string="defaultValue"):
print(string)
test()
A way to have the default value for a parameter as the default of another function: the Default singleton - Ideas - Discussions on Python.org
Is there a way to set a default value for an instance variable?
python - How do you set a default value for a variable in Python3? - Stack Overflow
Python variable value for default argument - Stack Overflow
Basically many years ago I was learning Ruby and I swear there was a way to set a default value when setting up a Class's variables, that could be overridden when creating an instance of a class. And if you didn't specify this instance variable when creating the object then the default class variable would remain. It was basically an optional instance variable.
Is there something similar to this in Python, or am I talking a bunch of nonsense and misremembering?
I'll give an example.
class Animal:
def __init__(self, name, age):
self.name = name
self.age = age
self.animal_type = "fish"
self.location = "ocean"
sammy = Animal("Sammy", 5)
So basically the animal_type and location are class variables, but is there a way for me to add those as optional instance variables, so that they can be changed when creating a new object, or if not specified then the class variable defaults - "fish" and "ocean" - just stay as is?
You can use:
def __init__(self, name='your_default_name'):
Then you can either create an object of that class with my_object() and it will use the default value, or use my_object('its_name') and it will use the input value.
Just set it like this, so for default will use 9 spaces
def __init__(self, name=' '):
self.type = "f:"
self.name = name
self.length = "0000"
self.colon = ":"
self.blocks = ["000", "000", "000", "000", "000", "000", "000", "000", "000", "000", "000", "000"]
More information
(This answer assumes that you cannot modify foo, and that you cannot use reflection or introspection to determine what the default argument value is.)
It's the absence of an argument, not any particular value used as an argument, that triggers the use of the default value. The only way you can produce nothing out of something is to unpack an empty mapping
foo(**{})
or an empty sequence
foo(*())
Both * and ** are part of the function-call syntax, though, not part of the argument value, so with a variable, it still looks like
b = {}
foo(**b)
b = ()
foo(*b)
If you want None to revert to a default value, the easiest way is to do the logic in the function itself.
def foo(a=None):
if a is None:
a = 100
print(a)