weird behavior wrapping global functions into classes
Les Schaffer
schaffer at optonline.net
Thu Jun 14 12:28:14 EDT 2001
On Thu, 14 Jun 2001 16:19:26 +0200, "Alex Martelli" <aleaxit at yahoo.com>
wrote:
>Whenever a function is accessed as an attribute of a class or
^
|___ python coded
>instance (and in the latter case, it's found in the class and
>not in the instance itself), it's transmuted into a method
>(unbound and bound, respectively).
<steam release>
$$!!@(*!#(#!!@!@!!@
what the ?????!!!!!
sssssssssssssssssss....ssss......s....
</steam release>
how come the built-in open doesnt get transmuted but shelve.open does? just
because its not "python-coded"???
anyway, here's how i will deal with this bit of nastiness in the future:
class NumStorage:
def __init__(self):
tup = (self.DB,)
if self.mode:
tup += (self.mode,)
self.db = apply(self.dbops['open'], tup)
def close(self):
self.db.close()
class NumFlat(NumStorage):
mode = 'w'
DB = 'flat.txt'
#silly hack
dbops = {'open': open}
print 'in NumFlat class definition, dbopen = ', dbopen # ok
def store(self, key, value):
self.db.write(array2string(value, precision=8))
class NumShelf(NumStorage):
mode = None
DB = 'test.dbm'
#silly hack
dbops = {'open': shelf_open}
print 'in NumShelf class definition, dbopen = ', dbopen # ok
def store(self, key, value):
self.db[key] = value
your Callable thingy looks okay, but if its that easy, some such thing
should be built right into Python so this issue is crystal clear and
upfront/outfront. i never asked for dbopen to be a class method, or so i
thought, yet i get hammered in an inconsistent way (built-in open: good,
python-coded shelve.open: bad).
anyway, .... thanks, alex....
les schaffer
More information about the Python-list
mailing list