why no user-def. attributes?

G

Guest

I appologize in advance for stupid question, which is:
why are user-defined attributes not allowed for builtin types?
[I guess I undestand why *instances* cannot have them (e.g. then every
dict would have a dict which would have a dict..), but this is a
different question]

I can imagine several answers, so I put here those that don't seem
satisfactory to me :)

1. You can subclass, eg.
class my_int(int): pass
and then do whatnot, like my_int.__getitem__=some_vicious_function

Here the problem is just that it's easier to write 4 than
my_int(4), or "ha" than my_string("ha") etc.

2. You would probably want to add new methods, so why don't you
define a function and then write e.g. dowhatIwant([1,2,3]) instead of
[1,2,3].dowhatIwant()

That's OK, except for the convenience of special methods like __add__ or
__iter__

3. It would lead to a confusing code

Oops, maybe I shouldn't have written this one..


Anyway, please let me know the true reason (which is perhaps technical)


Best whishes
Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top