class attributes & data attributes

Discussion in 'Python' started by james_027, Jun 20, 2007.

  1. james_027

    james_027 Guest

    hi everyone,

    I am now in chapter 5 of Dive Into Python and I have some question
    about it. From what I understand in the book is you define class
    attributes & data attributes like this in python

    class Book:

    total # is a class attribute

    def __init__(self):
    self.title # is a data attributes
    self.author # another data attributes

    To define class attributes is like defining a function in class, to
    define a data attributes is defining a variable inside the __init__
    method.

    what makes me confuse is this model from Django

    from django.db import models

    class Person(models.Model):
    first_name = models.CharField(maxlength=30)
    last_name = models.CharField(maxlength=30)

    I believe the first_name and last_name are data attributes? but why it
    is they look like a class attributes as being define.

    Thanks in advance for explaining

    james
     
    james_027, Jun 20, 2007
    #1
    1. Advertising

  2. james_027 wrote:

    > hi everyone,
    >
    > I am now in chapter 5 of Dive Into Python and I have some question
    > about it. From what I understand in the book is you define class
    > attributes & data attributes like this in python
    >
    > class Book:
    >
    > total # is a class attribute
    >
    > def __init__(self):
    > self.title # is a data attributes
    > self.author # another data attributes
    >
    > To define class attributes is like defining a function in class, to
    > define a data attributes is defining a variable inside the __init__
    > method.
    >
    > what makes me confuse is this model from Django
    >
    > from django.db import models
    >
    > class Person(models.Model):
    > first_name = models.CharField(maxlength=30)
    > last_name = models.CharField(maxlength=30)
    >
    > I believe the first_name and last_name are data attributes? but why it
    > is they look like a class attributes as being define.


    First of all, the common term for what you call a data attribute is
    is "instance attribute".

    Furthermore - you're right and wrong.

    The django-code above defines a model class, which has some class-attributes
    declaring the fields the database shall have. and the instances as well!

    So while the above clearly are class attributes, the ORM runtime of django
    will create instances of that class that have instance attributes of the
    same name.

    Something along these lines (albeit a contrived example):

    class Foo(object):
    bar = "baz"

    def __init__(self):
    for name, value in self.__class__.__dict__.items():
    if isinstance(value, str):
    setattr(self, name, "some other value")

    f = Foo()
    print f.bar

    Which should result in "some other value". But it's untested code above.

    Diez
     
    Diez B. Roggisch, Jun 20, 2007
    #2
    1. Advertising

  3. james_027 a écrit :
    > hi everyone,
    >
    > I am now in chapter 5 of Dive Into Python and I have some question
    > about it. From what I understand in the book is you define class
    > attributes & data attributes like this in python


    s/data/instance/

    > class Book:
    >
    > total # is a class attribute
    >
    > def __init__(self):
    > self.title # is a data attributes
    > self.author # another data attributes
    >
    > To define class attributes is like defining a function in class, to
    > define a data attributes is defining a variable inside the __init__
    > method.
    >
    > what makes me confuse is this model from Django
    >
    > from django.db import models
    >
    > class Person(models.Model):
    > first_name = models.CharField(maxlength=30)
    > last_name = models.CharField(maxlength=30)
    >
    > I believe the first_name and last_name are data attributes? but why it
    > is they look like a class attributes as being define.


    first_name and last_name are actually class attributes. AFAICT, they are
    descriptors[1] controlling access to the resultset returned by the db query.

    [1] cf the doc for the descriptor protocol on python.org. This is the
    feature that - amongst other things - allow Python to have a support for
    'computed attributes' (aka properties).



    HTH
     
    Bruno Desthuilliers, Jun 20, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,955
    Thomas Weidenfeller
    Oct 12, 2005
  2. christopher diggins
    Replies:
    16
    Views:
    792
    Pete Becker
    May 4, 2005
  3. DaveLessnau
    Replies:
    3
    Views:
    451
    Howard
    May 16, 2005
  4. Joseph Turian
    Replies:
    5
    Views:
    631
  5. Jayden
    Replies:
    16
    Views:
    565
    Steven D'Aprano
    Sep 29, 2012
Loading...

Share This Page