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()
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
Setting a default value for a variable in function in python - Stack Overflow
python - Set default value and type for parameter - 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
You can't really specify it directly in the argument field, but you can convert it right after the function declaration:
def profile(request, pk=0):
pk = int(pk)
#to do
It will throw an error if the passed value for pk cannot be converted to an int
EDIT: I spoke too soon, apparently you can do exactly as you did, just change things around:
def profile(request, pk: int = 0):
#to do
BTW: I just did a quick research for "specify type of argument python". Please try to research easy things like so first before asking a question, you'll get an answer quicker :)
My code works for any input type of pk: integer, string with integer, string without integer
import re
def intCheck(pk):
contains_number = bool(re.search(r'\d', pk))
if contains_number:
return int(re.search(r'\d+', pk).group())
else:
return 0
def profile(request, pk=0):
pk = intCheck(pk)
print(request + " " + str(pk))
profile('request', "232")
profile('request', 123)
profile('request', "no number")
Output:
request 232
request 123
request 0