Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it safe to use the python word "type" in my code?

Can I use the word type in my own code or is it reserved? My function header:

def get(     self,     region='Delhi',     city='Delhi',     category='Apartments',     type='For sale',     limit=60,     PAGESIZE=5,     year=2012,     month=1,     day=1,     next_page=None,     threetapspage=0, ): 
like image 637
Niklas Rosencrantz Avatar asked May 12 '12 23:05

Niklas Rosencrantz


People also ask

How do you make a type safe in Python?

You can enforce type safety in your programs manually by checking types of inputs. Because everything is an object, you can always create classes that derive from base classes, and use the isinstance function to verify the type (at runtime of course). Python 3 has added type hints, but this is not enforced.

Is Inpython a keyword?

The is keyword is used to test if two variables refer to the same object. The test returns True if the two objects are the same object. The test returns False if they are not the same object, even if the two objects are 100% equal. Use the == operator to test if two variables are equal.

What does type () do in Python?

Syntax of the Python type() function The type() function is used to get the type of an object. When a single argument is passed to the type() function, it returns the type of the object. Its value is the same as the object.

Can you specify types in Python?

Specify a Variable Type There may be times when you want to specify a type on to a variable. This can be done with casting. Python is an object-orientated language, and as such it uses classes to define data types, including its primitive types.


2 Answers

Using type as a keyword argument to a function will mask the built-in function "type" within the scope of the function. So while doing so does not raise a SyntaxError, it is not considered good practice, and I would avoid doing so.

like image 62
modocache Avatar answered Sep 25 '22 00:09

modocache


Neither. It's not a reserved word (a list of which can be found at http://docs.python.org/reference/lexical_analysis.html#keywords ), but it's generally a bad idea to shadow any builtin.

like image 31
Wooble Avatar answered Sep 25 '22 00:09

Wooble