473,386 Members | 1,630 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.

Implementing Tuples with Named Items

in the python cookbook 2nd edition, section 6.7 (page 250-251), there a problem
for implementing tuples with named items. i'm having trouble understanding how
one of commands work and hope someone here can explain what exactly is going on.
without copying all the code here, here is the gist of the problem:

from operator import itemgetter

class supertup(tuple):
def __new__(cls, *args):
return tuple.__new__(cls, args)

setattr(supertup, 'x', property(itemgetter(0)))
t = supertup(2, 4, 6)
t.x
2

i understand what itemgetter does,
i = itemgetter(0)
i((2, 3, 4))
2
i((4, 8, 12))
4


i understand what property does, and i understand what setattr does. i tested
this problem myself and it works, but i can't understand how t.x evaluates to 2
in this case. how does itemgetter (and property) know what tuple to use? in my
itemgetter sample, the tuple is passed to itemgetter so it's obvious to see
what's going on. but in the supertup example, it isn't obvious to me.
thanks,

bryan

Jan 10 '06 #1
1 1333
Bryan wrote:
in the python cookbook 2nd edition, section 6.7 (page 250-251), there a
problem
for implementing tuples with named items. i'm having trouble
understanding how one of commands work and hope someone here can explain
what exactly is going on.
without copying all the code here, here is the gist of the problem:

from operator import itemgetter

class supertup(tuple):
def __new__(cls, *args):
return tuple.__new__(cls, args)

setattr(supertup, 'x', property(itemgetter(0)))
>>> t = supertup(2, 4, 6)
>>> t.x
>>> 2

i understand what itemgetter does,
>>> i = itemgetter(0)
>>> i((2, 3, 4))
>>> 2
>>> i((4, 8, 12))
>>> 4
i understand what property does, and i understand what setattr does. i
tested this problem myself and it works, but i can't understand how t.x
evaluates to 2 in this case. how does itemgetter (and property) know what
tuple to use? in my itemgetter sample, the tuple is passed to itemgetter
so it's obvious to see what's going on. but in the supertup example, it
isn't obvious to me.


Perhaps it helps to see the "intermediate" steps between a standard property
definition and your setattr() example:
class supertup(tuple): .... def getx(self): return self[0]
.... x = property(getx)
.... gety = itemgetter(1)
.... y = property(gety)
.... z = property(itemgetter(2))
.... supertup.t = property(itemgetter(3))
setattr(supertup, "u", property(itemgetter(4)))
t = supertup(range(5))
t.u, t.t, t.z, t.y, t.x

(4, 3, 2, 1, 0)

class T:
def method(self): pass

and

class T:
pass

def method(self): pass
T.method = method

are both creating a class 'T' with a method 'method'. setattr() is only
needed if you don't know the attribute's name at compile time -- a method
is just and attribute of a class object.

Peter
Jan 10 '06 #2

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

Similar topics

3
by: Thorsten Kampe | last post by:
I found out that I am rarely using tuples and almost always lists because of the more flexible usability of lists (methods, etc.) To my knowledge, the only fundamental difference between tuples...
3
by: BJörn Lindqvist | last post by:
I like tuples alot. But in some situations you seem to be forced to access the elements of a tuple via its indexes and that is pretty ugly. For example: # make_color() returns a rgb tuple (r, g,...
66
by: Darren Dale | last post by:
Hello, def test(data): i = ? This is the line I have trouble with if i==1: return data else: return data a,b,c,d = test()
66
by: Mike Meyer | last post by:
It seems that the distinction between tuples and lists has slowly been fading away. What we call "tuple unpacking" works fine with lists on either side of the assignment, and iterators on the...
6
by: Bob Kochem | last post by:
I am looking for a way via code to access all the menu itmes on a given form. I am looking for something like the Forms or Controls collections, but for menu items. Is there such a thing or method?...
10
by: rshepard | last post by:
While working with lists of tuples is probably very common, none of my five Python books or a Google search tell me how to refer to specific items in each tuple. I find references to sorting a list...
12
by: rshepard | last post by:
I'm a bit embarrassed to have to ask for help on this, but I'm not finding the solution in the docs I have here. Data are assembled for writing to a database table. A representative tuple looks...
122
by: C.L. | last post by:
I was looking for a function or method that would return the index to the first matching element in a list. Coming from a C++ STL background, I thought it might be called "find". My first stop was...
2
by: ata | last post by:
Hi, I need to create a user control that has got a property named "Items" which is of StringCollection type. On the other hand, the collection is supposed to be populated declaratively through...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.