Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is using "type" as an attribute name a bad practice?

Tags:

python

django

I have the following django model:

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

Is using "type" as an attribute name considered a bad practice?

Here the attribute is not shadowing "type", so it's not the same question as this one

like image 808
Alvaro Avatar asked Jun 01 '15 14:06

Alvaro


2 Answers

There's nothing wrong with it. It's not a member of python's reserved keywords.

However, naming a method type() would probably be confusing...

like image 188
rnevius Avatar answered Oct 22 '22 23:10

rnevius


General rule is: don't use names that are taken (e.g. type, file, int, etc.) regardless of whether they're in a "reserved" keywords list or not (since python allows it, it's not really "reserved"). This is mainly important to avoid getting into trouble when you actually need to use the real object (without noticing that you overrode it locally).

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

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

like image 26
sirfz Avatar answered Oct 22 '22 21:10

sirfz