Unified Type/class

Pedro Rodriguez pedro_rodriguez at club-internet.fr
Sat Jan 26 11:24:21 EST 2002


"Jason Orendorff" <jason at jorendorff.com> wrote:

> Pedro Rodriguez wrote:
>> CORE language provides some features, if anyone (L1, L2) are allowed to
>> alter those features, what should Appl. developper expect from the
>> CORE. Even adding a method could be a problem. I consider that, when
>> requiring to augment CORE features, it should be dealt as part of the
>> library framework and not by altering the CORE directly.
> 
> Note that this is a general bit of engineering good sense.

Agreed. The questions are
- why did Ruby team allow this feature on standard classes ?
  (maybe 'str' is not a class on which Ruby interpreter relies ?)
- but if it is not the case, how do they address the problem ? 
But this is probably off topic in this ng.

> ... It's not
> something Python itself generally tries to enforce.
> 
> You can replace __builtin__.str with a Kumquat instance if you like. The
> core is completely hackable.

You almost convince me until I tried the following scripts (still I can't
explain it).

$ python str1.py
1
Hacked


# Python 1.5.2 -----------

$ python str2.py
Before
1
Traceback (innermost last):
  File "str2.py", line 2, in ?
    import str1
  File "str1.py", line 6, in ?
    __builtins__.str = f
TypeError: object has read-only attributes


# Python 2.2 -----------

$ python str2.py
Before
1
Traceback (most recent call last):
  File "str2.py", line 2, in ?
    import str1
  File "str1.py", line 6, in ?
    __builtins__.str = f
AttributeError: 'dict' object has no attribute 'str'

----------------------------------------------------------------- str1.py
----------------------------------------------------------------- def
f(x):
    return "Hacked"

print str(1)

__builtins__.str = f

print str(2)
-----------------------------------------------------------------

----------------------------------------------------------------- str2.py
----------------------------------------------------------------- print
str("Before")
import str1
print str("After")
-----------------------------------------------------------------

---
Pedro





More information about the Python-list mailing list