473,756 Members | 3,663 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.M odel):
first_name = models.CharFiel d(maxlength=30)
last_name = models.CharFiel d(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 2142
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.M odel):
first_name = models.CharFiel d(maxlength=30)
last_name = models.CharFiel d(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(valu e, 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.M odel):
first_name = models.CharFiel d(maxlength=30)
last_name = models.CharFiel d(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
2488
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 z.modulus() or modulus(z)? Is there a difference between Python and C++ in this regard? from math import sqrt class xy: def __init__(self,x,y): self.x = x
50
6375
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 def foo(self, data): self.attr1=data self.attr2.append(data) The initialization of attr1 is obviously OK, all instances of Father redefine it in the method foo. But the initialization of attr2 is wrong
3
2584
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, ( oRecordSet ( oItem ).Attributes & 0x4) is always 0 for every field on SQL Server 2000 Example code: var sSQL = 'SELECT TOP 1 * FROM WHERE ' + sUniqueField + '=' + sUniqueValue + ';';
2
1418
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, each vendor has some attributes - which are not "simple" Basically, they can be an instance of another class
8
2818
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);" TextBox2.Attributes.Add("onKeyPress", jscode) You will notice that jscode contains the JavaScript Logical And operator (&&). However, ASP.NET renders this as &amp;&amp; in the code that is
11
2785
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 < or several others, it converts them to html, such as &amp; or &lt; which can sometimes cause my scripts not to work. How can I prevent ASP.NET from doing this? Thanks. -- Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/
7
13703
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
1882
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 attributes to certain outcomes)", where "object1" and "object2" are generic objects, and the "conditions" and "outcomes" are dynamic run- time inputs? Typically, logic code for any application out there is hard-coded. I have been working with...
0
9456
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9273
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10032
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8712
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7244
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6534
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5141
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5303
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2666
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.