473,545 Members | 2,779 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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__(c ls, args)

setattr(supertu p, 'x', property(itemge tter(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 1336
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__(c ls, args)

setattr(supertu p, 'x', property(itemge tter(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 "intermedia te" 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(itemge tter(2))
.... supertup.t = property(itemge tter(3))
setattr(supertu p, "u", property(itemge tter(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
2108
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 and lists is that tuples are immutable, so if this is correct, than list are a superset of tuples, meaning lists can do everything tuples can do and...
3
1728
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, b). I.e. (255, 0, 0) print "The red value is: ", make_color() Not nice at all. It is maybe OK for a rgb tuple that only has three elements, but...
66
4909
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
3463
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 values side. IIRC, "apply" used to require that the second argument be a tuple; it now accepts sequences, and has been depreciated in favor of *args,...
6
4942
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? I would prefer to avoid using the Win API but will do so if that's the only way available. This is for a language-translation (of an existing...
10
5135
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 of tuples, but not extracting tuples based on their content. In my case, I have a list of 9 tuples. Each tuple has 30 items. The first two items...
12
4142
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 like this: ('eco', "(u'Roads',)", 0.073969887301348305) Pysqlite doesn't like the format of the middle term: pysqlite2.dbapi2.InterfaceError:...
122
5413
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 the Sequence Types page of the Library Reference (http://docs.python.org/lib/typesseq.html); it wasn't there. A search of the Library Reference's...
2
1185
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 the aspx file. It would be highly appreciated if you could tell me how I am supposed to do so. Regards,
0
7689
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. ...
1
7456
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7786
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6022
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...
1
5359
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...
0
3470
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1919
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1044
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
743
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...

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.