OO in Python? ^^

Magnus Lycka lycka at carmen.se
Wed Dec 14 04:52:08 EST 2005


bonono at gmail.com wrote:
> Magnus Lycka wrote:
> 
>>I don't really know Haskell, so I can't really compare it
>>to Python. A smarter compiler can certainly infer types from
>>the code and assemble several implementations of an
>>algorithm, but unless I'm confused, this makes it difficult
>>to do the kind of dynamic linking / late binding that we do in
>>Python. How do you compile a dynamic library without locking
>>library users to specific types?
> 
> I don't know. I am learning Haskell(and Python too), long way to go
> before I would get into the the usage you mentioned, if ever, be it
> Haskell or Python.

Huh? I must have expressed my thoughts badly. This is trivial to
use in Python. You could for instance write a module like this:

### my_module.py ###
import copy

def sum(*args):
     result = copy.copy(args[0])
     for arg in args[1:]:
         result += arg
     return result

### end my_module.py ###

Then you can do:

 >>> from my_module import sum
 >>> sum(1,2,3)
6
 >>> sum('a','b','c')
'abc'
 >>> sum([1,2,3],[4,4,4])
[1, 2, 3, 4, 4, 4]
 >>>

Assume that you didn't use Python, but rather something with
static typing. How could you make a module such as my_module.py,
which is capable of working with any type that supports some
standard copy functionality and the +-operator?



More information about the Python-list mailing list