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
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