Super Tuples

Eugene Goodrich bitbucket at isomedia.com
Wed Dec 29 13:33:59 EST 1999


Well, that does answer my question.

I guess I'm on the side for immutable dictionaries, too, but I'll be
at the back of the room for that one because I'm wary of asking for
pandora's box to be opened.  (I really don't know how tough it would
be, you see.)

Begging your pardon, but is it possible to make a class that looks
tuplish to users trying to access it by index but also exposes its
values via .properties?  What incompatibilities with functions
expecting tuples would this code provide: (sorry for any bad wrapping)

->>>
import types

class tuplish:
	def __init__ (self):
		self._data = ['alpha', 100, 'charlie']
		self._props = ['header', 'body', 'footer']
		self._propMap = {}
		# appologies for any hurt eyes on this next part.  It
_does_ separate the hide from the cat, tho.
		for iCounter in range (len (self._props)):
			(sProp, unkVal) = (self._props [iCounter],
self._data [iCounter])
			exec 'def fFunc (self, unkVal): self.%s =
unkVal' % sProp
			fFunc (self, unkVal)
			exec "self._propMap [%d] = 'self.%s'" %
(iCounter, sProp)
	def __len__ (self):
		return (len (self._data))
	def __getitem__ (self, iIndex):
		return (self._data [iIndex])
	def __setitem__ (self, iIndex, unkNewVal):
		self._data [iIndex] = unkNewVal
		exec ('def fFunc (self, unkVal): %s = unkVal' %
self._propMap [iIndex])
		fFunc (self, unkNewVal)
		return (unkNewVal)
	def __str__ (self):
		return (str (tuple (self._data)))

def foo (tTuple):
	print 'foo will now rifle through what it thinks is a
tuple...'
	for iCounter in range (len (tTuple)):
		print '\t%d: %s' % (iCounter, tTuple [iCounter]),
	print
	for unkThingie in tTuple:
		print '\t', unkThingie,

# test the class
o = tuplish ()
print 'Converted to tuple: %s' % str (tuple (o))
print 'Making some changes...'
for iCounter in range (len (o)):
	unkVal = o [iCounter]
	if type (unkVal) == types.IntType:
		unkVal = unkVal * 3.145
	else:
		unkVal = unkVal + '!'
	o [iCounter] = unkVal
print 'As string: %s' % o
print 'Methods:'
for sProp in dir (o):
	if sProp [0] == '_':
		continue
	print '\t.%s:  \t%s' % (sProp, str (eval ('o.%s' % sProp)))
foo (o)
<<<-

    -Eugene

P.S. please consider the above a prototype, not something I would
necessarily be proud of using.  I'm sure keeping each datum in 2
places and creating temporary functions with exec () all the time is
not a good thing... :)

On Wed, 29 Dec 1999 08:44:23 -0500, Paul Prescod <paul at prescod.net>
wrote:

>Eugene Goodrich wrote:
>> 
>> When I started in Python, my PyMentor described tuples as immutable
>> lists.  When they start acting like much more than that, isn't it time
>> to use an object?
>
>I agree that tuples in Python are often used merely as immutable lists.
>This bothers me for several reasons:
>
> * that isn't what "tuple" means in mathematics. In most mathematics
>EVERYTHING is immutable. Tuples and lists are still distinct
>
> * why does Python need an immutable list type but not an immutable
>dictionary?
>
> * isn't immutability more a property of where an object is used than
>the objects actual type? For example, don't you sometimes want to pass a
>list to a function but guarantee that the list won't be modified?
>
> * it is because of this abuse that the "one-item tuple" problem arises
>so often. There is no such problem in mathematics because a one-item
>tuple does not make sense.
>
>In Python world we most often use tuples as mathematicians do. Really, a
>tuple is supposed to be a collection of things where the sender and the
>recieiver have agreed on the number of items in advance, and every item
>has a special "meaning." The time library is a perfect example of this:
>
>"The time tuple as returned by gmtime(), localtime(), and strptime(),
>and accepted by asctime(), mktime() and strftime(), is a tuple of 9
>integers: year (e.g. 1993), month (1-12), day (1-31), hour (0-23),
>minute (0-59), second (0-59), weekday (0-6, monday is 0), Julian day
>(1-366) and daylight savings flag (-1, 0 or 1)."
>
>The primary weakness with the time library is that you have to index
>daylight savings flag by (for example) [9] instead of .daylight .
>
>If you use tuples as mathematicians do then single-item tuples can never
>arise because senders and recievers would never agree on a "protocol"
>(used loosely) that involves 1 length tuples (why use a tuple!).
>
> Paul Prescod
>

import binascii; print binascii.a2b_base64 ('ZXVnZW5lQGlzb21lZGlhLmNvbQ==')



More information about the Python-list mailing list