473,386 Members | 1,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

class attributes & data attributes

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

Jun 20 '07 #1
2 2123
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
Jun 20 '07 #2
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
Jun 20 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: beliavsky | last post by:
What are the pros and cons of defining a method of a class versus defining a function that takes an instance of the class as an argument? In the example below, is it better to be able to write...
50
by: Dan Perl | last post by:
There is something with initializing mutable class attributes that I am struggling with. I'll use an example to explain: class Father: attr1=None # this is OK attr2= # this is wrong...
3
by: Dan Sikorsky | last post by:
How can I get the recordset attributes for a table field in SQL Server 2000 to report the field updatable attribute correctly ... mine keeps saying the fields are not updatable? That is, (...
2
by: Kalpesh | last post by:
Hello, I am facing a design problem here & need your expert design advice on this. Scenario: I have a class called Vendor - which has several simple attributes like Name, Address etc Now,...
8
by: Nathan Sokalski | last post by:
I add a JavaScript event handler to some of my Webcontrols using the Attributes.Add() method as follows: Dim jscode as String = "return (event.keyCode>=65&&event.keyCode<=90);"...
11
by: Nathan Sokalski | last post by:
I add several JavaScript events (onchange, onkeypress, etc.) to Controls using the Add method of the Attributes collection. However, if the JavaScript code contains certain characters, such as & or...
7
by: S. Lorétan | last post by:
Hi guys, Sorry for this stupid question, but I don't know why it isn't working. Here is my (example) code: namespace Test { class A { public string Label1; }
4
by: Basilisk96 | last post by:
This topic is difficult to describe in one subject sentence... Has anyone come across the application of the simple statement "if (object1's attributes meet some conditions) then (set object2's...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.