Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > I found some very odd behaviour in Python's very basic types

Reply
Thread Tools

I found some very odd behaviour in Python's very basic types

 
 
Sunjay Varma
Guest
Posts: n/a
 
      03-10-2011
For some reason, sub-classing and overwriting a built-in type does not
change the behavior of the literal. Logically speaking, overwriting a
name, such as str, should delete the basic str type, and replace it
with the new class or object put in its place. For some reason though,
even though the interpreter says that str == type("hello"),
overwriting the str name changes nothing in the literal. Is this a
bug? I'm not sure.

I also have just started this very discussion (although in more words)
on Python-Forum.org. Check it out there as well:
http://python-forum.org/pythonforum/...24542&p=113404

I know a lot of very experienced Python programmers view these mailing
lists. It would be fantastic if this could get fixed. Python would get
momentously more powerful if this feature was implemented. People
would be able to apply new methods and attributes to strings, lists,
dictionaries, sets, and all the built-in types. Improving them when
needed and allowing for extended functionality.

If you prefer to email me directly, just use this email:
haimunt(at)yahoo(dot)com

Thanks for your help! I hope we can discuss this (possible) bug.

-Sunjay03
 
Reply With Quote
 
 
 
 
Terry Reedy
Guest
Posts: n/a
 
      03-10-2011
On 3/10/2011 12:47 AM, Sunjay Varma wrote:
> For some reason, sub-classing and overwriting a built-in type does not
> change the behavior of the literal. Logically speaking, overwriting a
> name, such as str, should delete the basic str type, and replace it
> with the new class or object put in its place.


No, that is fundamentally wrong. Rebinding a name in a particular
namespace only deletes the association in that namespace.

Built-in classes are just that. You cannot get rid of them. They are
used in the operation of the interpreter.

> For some reason though,
> even though the interpreter says that str == type("hello"),
> overwriting the str name changes nothing in the literal.


Right. By design. String and number literals are always string and
number literals.

> Is this a bug?


No.

--
Terry Jan Reedy

 
Reply With Quote
 
 
 
 
rusi
Guest
Posts: n/a
 
      03-10-2011
On Mar 10, 10:47*am, Sunjay Varma <varma.sun...@gmail.com> wrote:
> For some reason, sub-classing and overwriting a built-in type does not
> change the behavior of the literal.


Have you looked through this?
http://www.python.org/download/relea...o/#subclassing

[Dunno exactly how current it is -- This was at 2.2, most people are
likely to be at 2.6 now or later. Maybe others can say...]
 
Reply With Quote
 
scattered
Guest
Posts: n/a
 
      03-10-2011
On Mar 10, 12:47*am, Sunjay Varma <varma.sun...@gmail.com> wrote:
> For some reason, sub-classing and overwriting a built-in type does not
> change the behavior of the literal. Logically speaking, overwriting a
> name, such as str, should delete the basic str type, and replace it
> with the new class or object put in its place. For some reason though,
> even though the interpreter says that str == type("hello"),
> overwriting the str name changes nothing in the literal. Is this a
> bug? I'm not sure.
>
> I also have just started this very discussion (although in more words)
> on Python-Forum.org. Check it out there as well:http://python-forum.org/pythonforum/...24542&p=113404
>
> I know a lot of very experienced Python programmers view these mailing
> lists. It would be fantastic if this could get fixed. Python would get
> momentously more powerful if this feature was implemented. People
> would be able to apply new methods and attributes to strings, lists,
> dictionaries, sets, and all the built-in types. Improving them when
> needed and allowing for extended functionality.
>
> If you prefer to email me directly, just use this email:
> haimunt(at)yahoo(dot)com
>
> Thanks for your help! I hope we can discuss this (possible) bug.
>
> -Sunjay03


I agree with the others that this is a desirable feature rather than a
bug. For one thing, most nontrivial scripts import at least one
module. Those modules will implicitly assume that, e.g., string
literals mean what the language specification says that they mean. If
you were able to change the built-in meaning of strings then how would
these modules function? If you say that they are in their own
namespace and wouldn't be effected by your renaming of str - then you
would still have what you call the odd behavior of the original
meaning of built-in types leaking through your attempt to redefine
them. On the other hand - if the change in basic types *did* apply to
the code in imported modules - that would almost certainly break a lot
of modules. At best you would have some of the murky semantics of
dynamic typing, where the actual meaning of code can't be determined
lexically but would depend on the calling sequence.

In any event - why would you want to change the meaning of built-in
types? I've always thought of OOP as a means to extend a language, not
rewrite it.
 
Reply With Quote
 
John Roth
Guest
Posts: n/a
 
      03-10-2011
On Mar 9, 10:47*pm, Sunjay Varma <varma.sun...@gmail.com> wrote:
> For some reason, sub-classing and overwriting a built-in type does not
> change the behavior of the literal. Logically speaking, overwriting a
> name, such as str, should delete the basic str type, and replace it
> with the new class or object put in its place. For some reason though,
> even though the interpreter says that str == type("hello"),
> overwriting the str name changes nothing in the literal. Is this a
> bug? I'm not sure.
>
> -Sunjay03


This is neither a bug nor a feature, it's simply the way that
Python works. Literals are handled during compilation; the built-in
types are run-time objects.

Python is not a language where a script can change compile-time
behavior. Doing that would make it a very different language, and
would put it into a very different niche in the language ecology.

John Roth
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
A very **very** basic question mdh C Programming 57 09-26-2008 03:25 PM
Odd behavior with odd code Michael Speer C Programming 33 02-18-2007 07:31 AM
very very basic question aghazalp Python 6 04-02-2006 09:35 PM
Some more odd behaviour from the Regexp library David Veerasingam Python 4 10-20-2005 04:01 PM
Very very very basic question Peter C Programming 14 02-14-2005 09:46 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57