Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Class Members and Instance Members in Django and "Ordinary" Python?

Tags:

Beginner question here! Some time ago, I asked this question: Parse CSV records into a list of Classes, which was also answered more technically here: How do I avoid having class data shared among instances?

I learned that, in Python classes, variables that will be defined on a per-object basis need to be declared in the __init__(self) function.

So for:

class ClassOne:
    def __init__(self, datetime):
        self.datetime = datetime
    v = []

the variable v will hold the same data for all instances of ClassOne, whereas for:

class ClassTwo:
    def __init__(self, datetime):
        self.datetime = datetime
        self.v = []

variable v holds individual data for each instance of ClassTwo.

However, in Django (which I'm learning now), I see the "normal" (more C++ like) behavior again for the variables:

class Post(models.Model):
    title = models.CharField(max_length = 255)

Here, the variable title holds individual data for each instance of Post, despite not being defined in the __init__ function.

My basic question is Why or How does title pertain to individual class objects instead of being common to every class object, as v in ClassOne is above?

If I'm understanding this right, this means that Django classes are interpreted differently than normal Python classes? However, that conclusion doesn't make sense...

I hope that someone can help me understand this. It was my assumption previously that python code (say, a data analysis or a scientific model) could be built into a web-based service by using it's classes and routines in a Django app. If the implementation of the two different classes is different, then this would be quite difficult!

This may have been answered elsewhere. I'm not well versed in Django jango, so don't know what to search for.