9

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

6

3 Answers 3

8

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...

4
  • Even though it works I wouldn't say there's nothing wrong with it. It's potentially confusing to the reader at a minimum.
    – 101
    Jun 1, 2015 at 14:17
  • 4
    How is it more confusing than the way Django uses id? id() is also a builtin...
    – rnevius
    Jun 1, 2015 at 14:18
  • 3
    Yeah I don't think it confuses anyone since there is no chance of accessing it without naming the class or an instance (eg. SomeProfile.type or instance.type)
    – Alvaro
    Jun 1, 2015 at 14:22
  • Methods are just attributes of objects which contain functions. You contradict yourself. Don't use builtin names, once you'll run into a situation when you'll need to name a regular variable as attribute of your object.
    – validname
    Jan 7, 2020 at 14:17
6

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).

0

Yes - its bad practice. type is very general word from keyword perspective though not a reserved keyword. Even though its not giving any problems currently in your application but it might give in future as it could have been used in some already existing libraries or python extensions.

Example: type being used as a function to get TypeCast information of variable

name = "John"
age = 12

print type(name)
## Above line will return "<type 'str'>"

print type(age)
## Above line will return "<type 'int'>"

Usage of type being used as an attribute is a bad practice.

3
  • those are functions, not attributes
    – Alvaro
    Jun 1, 2015 at 14:11
  • 1
    Ofcourse - its function. But naming your properties as type will surely create confusion. When we already know there is a function named type then I recommend not use it as a property name. This can be considered as a best practice. Jun 1, 2015 at 14:13
  • 1
    Question was about - whether that will be a bad practice or not. As a developer you can always name the property as type (wherein Python wont stop you) but when we think about the USABILITY + MAINTAINABILITY + READABILITY its surely a bad practice. Most importantly if something is not going bring any significance to coding practice then why to do that. Instead of type one can always use something category_type || entity_type which would more meaningful as well. Jun 1, 2015 at 14:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.