Does "type" use bad practice as an attribute name?

I have the following django model:

class SomeProfile(models.Model): type = models.CharField(max_length=1) 

Does "type" use an attribute name considered bad practice?

Here the attribute does not obscure the "type", so this is not the same question as this

+5
source share
3 answers

There is nothing wrong. It is not included in python-enabled keywords .

However, the name of the type() method is likely to be confused ...

+6
source

General rule: do not use names that are accepted (for example, type , file , int , etc.), regardless of whether they are in the list of reserved keywords or not (since python allows this, it really is not "reserved" ) This is mainly important to avoid getting into trouble when you really need to use a real object (without noticing that you are redirecting it locally).

If you really want to use one of these names, just add _ to the end (e.g. type_ ).

In your case, since you specify type as an attribute of a class, it should be considered safe, since it can only be accessed through its class ( self.type or SomeProfile.type ).

+3
source

Yes - its a bad practice. type is a very general word in terms of the keyword, but not a reserved keyword. Despite the fact that it does not give any problems at present in your application, but it can give in the future, since it can be used in some existing libraries or python extensions.

Example: type is used as a function to get the TypeCast variable information

 name = "John" age = 12 print type(name) ## Above line will return "<type 'str'>" print type(age) ## Above line will return "<type 'int'>" 

Using type , used as an attribute , is bad practice.

+1
source

All Articles