Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python3: pass type as function parameter

How can i pass int or str as function parameters in python?

Example:

def fn(type:<arg typing here>):
    # do something

output = fn(type=int)

Example 2 from django commands:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

parser.add_argument(
        'years', type=int, help="Determines how many years of fake data to generate.")

>Solution :

Other than the typing declaration, your code should already work. For typing the parameter, you could use typing.Type:

from typing import Type

def fn(type: Type):
    # do something

However, you might not want to use the builtin name type as the name of a parameter. Some might prefer something like this:

from typing import Type

def fn(typ: Type):
    # do something
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading