OO in Python? ^^

Antoon Pardon apardon at forel.vub.ac.be
Wed Dec 14 06:40:51 EST 2005


Op 2005-12-14, Magnus Lycka schreef <lycka at carmen.se>:
> 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.

That depends on what you would call static typing.

Suppose we would add type declarations in python.
So we could do things like

  int: a
  object: b

Some people seem to think that this would introduce static
typing, but the only effect those staments need to have
is that each time a variable is rebound an assert statement
would implicitly be executed, checking whether the variable is
still an instance of the declared type.

(Assuming for simplicity that all classes are subclasses
of object so that all objects are instances of object.)

> How could you make a module such as my_module.py,

In the above scenario in just the same way as in python.

-- 
Antoon Pardon



More information about the Python-list mailing list