CONSTRUCT - Adding Functionality to the Overall System

MonkeeSage MonkeeSage at gmail.com
Wed Sep 20 08:39:05 EDT 2006


Ilias Lazaridis wrote:
> no, I don't know it.

OK...so how do you evaluate a language when you don't know its basic
operations? Hmmm, sounds fishy.

> how do I define something into the top-level namespace? I assume I
> could place it into the root-package of my project, into the __init__
> function.
>
> But how do I place it within my computers python installation (the big
> init)?

When you just say:

def blah(): pass

Now 'blah' function is in the top-level namespace, just like global
variables. Or if it's in a different file, you'd say 'from blah import
*'. You honestly don't know this?!??

> I am aware of this technique.
>
> But I want to modify existent classes, without touching their code.

The only way to do this is to explicitly subclass the existent classes
with your own class and modify what you want there in your subclass
(see below), or use multiple inheritence as I suggested previously.

> I've noticed some interesting code on you website:
>
> "
> class file(file):
>   def reopen(self, name=None, mode='w', bufsize=None):
> ...
>
> fh = file('test.txt', 'rb')
> print fh # <open file 'test.txt', mode 'rb' at 0xb7c92814>
> fh.reopen(mode='wb')
> "
> http://rightfootin.blogspot.com/2006/09/ruby-reopen.html
>
> does this mean that I can add a method to a class in a similar way with
> ruby? (by using class class-name(class-name): )

Basically, yes. In ruby you can reopen base classes; in python you can
get the same effect by subclassing the base classes with the same name
as the base class, then instantiating all your objects as that class.
This is the exact same idea as a "super-duper super class" as I
mentioned above.

> but the limitation is that I cannot do this with the python build-in
> types?:
>
> http://rightfootin.blogspot.com/2006/08/of-rocks-and-reptiles.html

You can subclass buit-in types using the same name as the parent class.
In fact here is what I use:

## my little library to make python more OO
## i.e., to get rid of the top-level junk...
## or at least to hide it behind some pretty
## object attributes :)
##
## YMMV - and don't complain if you don't
## like it; I wrote it for ME, not you
##
## Jordan Callicoat < MonkeeSage at gmail.com >

## some global methods so we can use them
## to set up the class methods
def rpr(self):
  return str(repr(self))
def stri(self):
  return str(self)
def inte(self):
  return int(self)
def leng(self):
  return int(len(self))
def say(self):
  print(self)
  return self
def joi(self, sep=''):
  return str(sep.join(self))
def ma(self, m):
  return list(map(m, self))
def filt(self, m):
  return list(filter(m, self))
def redu(self, m):
  return list(reduce(m, self))


## now build all the subclasses
class int(int):
  """
  Wrapper class for +int+
  Provided methods:
    str(), int(), repr(), say(),
    len(), plus(), minus(),
    times(), divided_by()
  """
  def plus(self, i):
    return int(self + i)
  def minus(self, i):
    return int(self - i)
  def times(self, i):
    return int(self * i)
  def divided_by(self, i):
    return int(self / i)

int.str  = stri
int.repr = rpr
int.len  = leng
int.int  = inte
int.say  = say

class str(str):
  """
  Wrapper class for +str+
  Provided methods:
    str(), int(), repr(), say(),
    len(), plus(), times()
  """
  def plus(self, s):
    return str(self + s)
  def times(self, i):
    return str(self * i)

str.str  = stri
str.repr = rpr
str.len  = leng
str.int  = inte
str.say  = say

class list(list):
  """
  Wrapper class for +list+
  Provided methods:
    str(), int(), repr(), say(),
    len(), plus(), times(), join()
  """
  def plus(self, l):
    return list(self + l)
  def times(self, i):
    return list(self * i)

list.str    = stri
list.repr   = rpr
list.len    = leng
list.say    = say
list.join   = joi
list.map    = ma
list.filter = filt
list.reduce = redu

class tuple(tuple):
  """
  Wrapper class for +tuple+
  Provided methods:
    str(), int(), repr(), say(),
    len(), plus(), times(), join()
  """
  def plus(self, l):
    return tuple(self + l)
  def times(self, i):
    return tuple(self * i)

tuple.str    = stri
tuple.repr   = rpr
tuple.len    = leng
tuple.say    = say
tuple.join   = joi
tuple.map    = ma
tuple.filter = filt
tuple.reduce = redu

class dict(dict):
  """
  Wrapper class for +dict+
  Provided methods:
    str(), int(), repr(), say(),
    len()
  """
  pass

dict.str  = stri
dict.repr = rpr
dict.len  = leng
dict.say  = say
dict.join = joi


## done with the global methods, remove them
del(rpr, stri, inte, say, joi, ma, filt, redu)


## show examples if called standalone
if (__name__ == '__main__'):
  i = int(5)
  n = i + 5
  n.say()
  i = i.times(5).divided_by(3).plus(2)
  i.str().int().str().repr().say()

  s = 'Tree'
  i = str(s)
  i = i.plus(' and dog\n').times(2)
  i.repr().say()

  def test(item):
    return '%s!' % item
  l = ['test', 'this', 'out', 'now']
  i = list(l)
  i = i.times(2)
  i.join(' ').say()
  i.map(test).say()

  t = ('test', 'this', 'out', 'now')
  i = tuple(t)
  i = i.plus(('hi', 'there'))
  i.join('+').repr().say()
  i.map(test).say()

  d = {'test': 'this', 'out': 'now'}
  i = dict(d)
  i.len().say()

It's not the most "pythojnic" way to do things, but it works for me.

Ps. I still have a hard time belieiving that after all the time you've
spent on comp.lang.lisp, comp.lang.ruby and comp.lang.python you still
don't understand these basic concepts...if that's really true, I would
never use your consulting service!

Regards,
Jorda




More information about the Python-list mailing list