OO in Python? ^^
Mike Meyer
mwm at mired.org
Wed Dec 14 13:16:25 EST 2005
Magnus Lycka <lycka at carmen.se> writes:
> 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?
CLU had this decades ago. You'd right something like:
def sum(*args) args has +=:
...
Basically, it did duck typing, checked at compile time instead of
dynamically.
<mike
--
Mike Meyer <mwm at mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
More information about the Python-list
mailing list