Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use the variable name "type" as function argument in Python?

Tags:

Can I use type as a name for a python function argument?

def fun(name, type):     .... 
like image 801
bodacydo Avatar asked Mar 10 '10 15:03

bodacydo


People also ask

Can variable name be same as function name Python?

Bottom line: you can't have two things simultaneously with the same name, be it a function, an integer, or any other object in Python. Just use a different name.

Can a variable be an argument Python?

With Python, we can create functions to accept any amount of arguments. In this article, we will look at how we can define and use functions with variable length arguments. These functions can accept an unknown amount of input, either as consecutive entries or named arguments.

How do you pass a variable name as an argument in Python?

Use a dict. If you really really want, use your original code and do locals()[x][0] = 10 but that's not really recommended because you could cause unwanted issues if the argument is the name of some other variable you don't want changed. Show activity on this post. Show activity on this post.

How do you specify data types in a Python function argument?

Python is a strongly-typed dynamic language in which we don't have to specify the data type of the function return value and function argument. It relates type with values instead of names. The only way to specify data of specific types is by providing explicit datatypes while calling the functions.


1 Answers

You can, but you shouldn't. It's not a good habit to use names of built-ins because they will override the name of the built-in in that scope. If you must use that word, modify it slightly for the given context.

While it probably won't matter for a small project that is not using type, it's better to stay out of the habit of using the names of keywords/built-ins. The Python Style Guide provides a solution for this if you absolutely must use a name that conflicts with a keyword:

single_trailing_underscore_: used by convention to avoid conflicts with Python keyword, e.g.

Tkinter.Toplevel(master, class_='ClassName') 
like image 70
jathanism Avatar answered Oct 01 '22 01:10

jathanism