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 1 1346
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 more.
Is there any advantage for using tuples? Are they "faster"? Consume
less memory? When is...
|
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 for a tuple that has 10+ elements, index access is
rediculous. In that case, unpacking wont helpe...
|
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()
|
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, which
accepts not only sequences but iterators.
Is there any place in the language that still...
|
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 program) where I wish to
change (English to Spanish initially) the Descriptions for all the menu...
| |
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 are 3-character strings, the remaining 28 itmes are floats.
I want to create a new list from...
|
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: Error binding parameter 1 - probably
|
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 index seemed to confirm that the function did not
exist. A little later I realized it might be called...
|
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,
|
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...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
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 most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |