On 2 mar, 22:02, Alex Hall <mehg...@gmail.com> wrote:
> Hi all,
> Still working through that tutorial. I am just curious: why are none
> of the class variables called self.var, but rather just var? For
> example:
> import models
> class Poll(models.Model):
>  question=models.CharField(max_length=200)


What you define here is a class attribute, not an instance attribute.
FWIW, Django models do some metaprogramming magic with the models
fields, but that's rather advanced Python programming. You'll have to
read about Python's object model, metaclasses and descriptors if you
really want to understand what's going on here.

> Should that not be
> self.question=...
> instead?

"self" is nothing magical, it's just a coding convention for functions
meant to be used as bound methods. You could replace it with "parrot"
in your code, it would work just the same. So, "self" is only a name
like any other, usually used to refer to the object the function's
code is working on. This instance is passed to the function like any
other argument (even if there's some other metaprogramming magic
happening here). myobj.method() is just a convenient shortcut for
myobj.__class__.method(myobj) - and that's indeed what really happens.


> Otherwise, saying something like:
> poll=Poll()
> poll.question="question?"
> should not work;


For which definition of "should not work" ?-)

Python is a dynamic language. You can add / replace / remove instance
or class attributes (almost) at will:
>>> class Foo(object):
...     pass
...
>>> f = Foo()
>>> f.bar = 42
>>> f.bar
42
>>> f2 = Foo()
>>> f2.bar
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Foo' object has no attribute 'bar'
>>> del f.bar
>>> f.bar
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Foo' object has no attribute 'bar'
>>>

> question is not told to be part of the class by way
> of self.

'poll.question' is an ordinary instance attribute. The "question"
attribute defined in the Poll class is something very different.

> This may stray from django and into pure python territory,

Well, Django is a Python framework, so to understand Django you have
to understand Python...

> so
> sorry if it gets off-topic. I have never seen a class that does not
> tie its variables to itself

Looks like you're confusing class and instance. Every name defined at
the top level of a class statement's block become an attribute of the
class itself (yes, Python classes are objects too) - unless the
class's class (aka metaclass) do something weird with it when the
class statement is eval'd.


> with self or some other keyword.

self is NOT a keyword.

> Come to
> think of it, there is no __init__ method. I know that it is not
> necessary to have one, but I figured I would have seen one to get
> things set up.


There's of course a __init__ method, in the models.Model base class.
One of the nice things with Django is that it's opensource. This means
that you have the whole source code and the right to read it ;)

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to