3

I'm developing a django project for agriculture. I want to name an app "fields" and inside the app "fields" I want to name a model "Field" (referring to a farmer field).

I tried it and it works, so I assume that "fields" and "Field" are not reserved words in Django or Python. But I was just wondering if using these words can be problematic in the future or it's just fine?

And the general question: Is there any way to check if a word is reserved in Django or Python?

ferrangb
  • 2,012
  • 2
  • 20
  • 34
  • This can be resolved with either a quick google search or simply paying attention to what happens if you type the word(s) into an interpreter. – PyNEwbie Jul 02 '14 at 16:25
  • 1
    Accepted answer of: http://stackoverflow.com/questions/1962559/how-can-i-get-all-of-python-reserved-words-which-has-4-underline-str-becaus – 1478963 Jul 02 '14 at 16:27
  • also [here](http://stackoverflow.com/questions/22864221/is-the-list-of-python-reserved-words-available-in-a-library) – PyNEwbie Jul 02 '14 at 16:29
  • Thank you for the links. Now I know the Python reserved words. How can I know the django reserved words? – ferrangb Jul 02 '14 at 16:33
  • The problem with languages like Python is, you can use keywords as local variable names, and you would not feel the pinch unless you need the default type (which you refer as reserved words ) - AFAIK, `fields` and `Field` are not reserved. The closest you would get is, `forms.CharField`, etc.. – karthikr Jul 02 '14 at 16:47
  • @karthikr You **cannot** use *keywords* as identifers. You can use name of *built-ins*. But so you can do in any other language by simply avoiding to include/import the default built-in names. This can be done in C, or in Haskell without many problems. – Bakuriu Jul 02 '14 at 21:49

2 Answers2

2

Strictly speaking, "reserved word" is the wrong term here. Reserved words are properties of a programming language, and Django is not a language.

There is a class called Field in Django in the django.db.models.fields package, and another (similar but different) class with the same name in the django.forms.fields package.

However, these Field classes are so low level classes that it's unlikely that you will ever need to import them, so they won't interfere with your own Field class.

In any case, and as you can see, within Django itself the same class can exist in multiple packages with the same name. It's fine, because the package name serves as a namespace, so you can always fully qualify a class by using its complete package name.

Another useful thing in Python is importing classes with a different name:

from django.db.models.fields import Field as DjangoField
help(DjangoField)

Similarly, importing packages with a different name:

from django.db.models import fields as djangofields
help(djangofields.Field)

This way you can always avoid collisions in class and package names.

janos
  • 120,954
  • 29
  • 226
  • 236
2

little explanation with fun

Answer is Simply No,

Because Language only has the authority to own anything.Python is the owner of the house The Django guy is paying rent to Python guy. So, How Django guy can reserve the objects of the house?

same logic is applied here too

Community
  • 1
  • 1
Nava
  • 6,276
  • 6
  • 44
  • 68