Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set default value and type for parameter [duplicate]

How can I do something like this:

def profile(request, pk=0 : int):
    #to do

I need pk to be int (without converting in function).

Like this:

def profile(request, pk: int):

If pk empty - set value to 0 and type to int.

like image 243
paskalnikita Avatar asked Oct 19 '25 20:10

paskalnikita


1 Answers

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 :)

like image 57
Kai Avatar answered Oct 22 '25 12:10

Kai



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!