Can I use type
as a name for a python function argument?
def fun(name, type): ....
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.
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.
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.
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.
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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With