Thursday, August 8, 2013

Re: Why can't a Model have the same name as a View?

Python variables don't have types.  They're just references.

You can call models and views by the same name.  But a bare name can only have one value in a given python scope.

Thus, if in your views.py you do:

    from models import Foo

you have set the view module's global scope's variable 'Foo' to be a reference to that imported (presumably) models.Model subclass.  If later you do:

    def Foo(request):
        ...

then you have stored a new value in the view module's global scope's variable 'Foo', a reference to a function.  Which you will get upon reference depends on when (temporally, not lexically) you make the reference with respect to when the function got defined.

On the other hand, you could do:

    import models
    def Foo(request):
        ...

And later 'Foo' will always refer to the function while 'models.Foo' will always refer to the class.

Of course, the accepted convention is to name functions and methods all lower case (or beginning with a lower case letter, at least, while class names begin with an upper case letter.  If you follow this community standard, your issue never arises.  If you insist on bringing the naming conventions of another language into python, realize that other folks won't want to work on, review, or help with your code.

Bill


On Thu, Aug 8, 2013 at 2:13 PM, JJ Zolper <codingatmt@gmail.com> wrote:
Hello everyone,

I've only been working with Python and Django for a couple of years now and whenever I can I like to learn more about certain rationalizations for certain decisions made within each of them.

In Django we define a Python class with a name to represent our model (I think it's a python class at least) and then we write "def" for definition of a view function or python function. To me I view this as two separate types of structures and thus fairly often I give a model the same name as a view. It isn't until later that I realize that my app isn't working because of the fact that they have the same name. I'm here just wondering if anyone would be willing to explain how this comes about from how Python/Django treats this instance? Why does Django see a confliction between a class and a function with the same name? Aren't they entirely separate entities?

I'm by all means okay with going in and changing the names of the view or model to something slightly different so everything works, I just would like to understand conceptually why it is an issue?

Thanks a lot,

JJ Zolper

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate