Hello,
I found this recipe at the Python cookbook: http://aspn.activestate.com/ASPN/Coo...n/Recipe/81732
Saucily I tried to apply this to int or str, with this result:
TypeError: object does not support item assignment
Any way to go round that?
Reinhold
--
Wenn eine Linuxdistribution so wenig brauchbare Software wie Windows
mitbrächte, wäre das bedauerlich. Was bei Windows der Umfang eines
"kompletten Betriebssystems" ist, nennt man bei Linux eine Rescuedisk.
-- David Kastrup in de.comp.os.unix.linux.misc 7 2401
Reinhold Birkenfeld wrote: I found this recipe at the Python cookbook:
http://aspn.activestate.com/ASPN/Coo...n/Recipe/81732
Saucily I tried to apply this to int or str, with this result:
TypeError: object does not support item assignment
Any way to go round that?
int and str have no __dict__, neither in the class nor instance, and hence
no place to store your additions: "__dict__" in dir(4)
False "__dict__" in dir(4 .__class__)
False
Therefore you have to subclass them to gain the ability to add methods:
class Int(int):
.... def __new__(cls, n):
.... return int.__new__(cls, n)
.... x = Int(2) x.twice()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: 'Int' object has no attribute 'twice'
Now let's grow our Int class a twice() method:
def twice(self): return 2*self
.... Int.twice = twice x.twice()
4
I don't know for sure, but I suppose the tricks used in the recipe are no
longer necessary in current Python. Anyway, if you want to tack your custom
methods on the builtin integer and string classes, you're out of luck.
Peter
Peter Otten wrote: int and str have no __dict__, neither in the class nor instance, and hence no place to store your additions:
"__dict__" in dir(4) False "__dict__" in dir(4 .__class__) False
So why does "print int.__dict__" produce a value?
Therefore you have to subclass them to gain the ability to add methods: class Int(int): ... def __new__(cls, n): ... return int.__new__(cls, n) ... x = Int(2) x.twice() Traceback (most recent call last): File "<stdin>", line 1, in ? AttributeError: 'Int' object has no attribute 'twice'
Now let's grow our Int class a twice() method: def twice(self): return 2*self ... Int.twice = twice x.twice()
4
That's clear, yes. But the problem with this solution is that I must
convert all literals to Int before using them.
Reinhold
--
Wenn eine Linuxdistribution so wenig brauchbare Software wie Windows
mitbrächte, wäre das bedauerlich. Was bei Windows der Umfang eines
"kompletten Betriebssystems" ist, nennt man bei Linux eine Rescuedisk.
-- David Kastrup in de.comp.os.unix.linux.misc
Reinhold Birkenfeld wrote: So why does "print int.__dict__" produce a value?
Because I was wrong, I suppose. Anyway, there is code in descrobject.c that
wraps a dictionary/sequence type to ensure that it is readonly.
As to why it doesn't show up in dir() and why it is read-only, I have no
idea.
Peter
Peter Otten wrote: Reinhold Birkenfeld wrote:
So why does "print int.__dict__" produce a value?
Because I was wrong, I suppose. Anyway, there is code in descrobject.c that wraps a dictionary/sequence type to ensure that it is readonly. As to why it doesn't show up in dir() and why it is read-only, I have no idea.
So would that be a point where to make improvements?
Reinhold
--
Wenn eine Linuxdistribution so wenig brauchbare Software wie Windows
mitbrächte, wäre das bedauerlich. Was bei Windows der Umfang eines
"kompletten Betriebssystems" ist, nennt man bei Linux eine Rescuedisk.
-- David Kastrup in de.comp.os.unix.linux.misc
Reinhold Birkenfeld wrote: So would that be a point where to make improvements?
I'm not sure what you mean. Do you think it would be an improvement if int
and str could be customized? I doubt that. I can easily think of mutually
exclusive extensions that would prevent modules from different authors from
working together. If you have a compelling addition, why not suggest it for
inclusion in the next version of Python?
Another option is to adopt quixote's technique of turning str into htmltext.
Peter
PS: If you know whether read-only dictionaries are a technical or political
decision, don't hold back to tell us.
Peter Otten <__*******@web.de> writes: PS: If you know whether read-only dictionaries are a technical or political decision, don't hold back to tell us.
technical.
Cheers,
mwh
--
<faassen> I'm not a PSU agent.
-- from Twisted.Quotes
Reinhold Birkenfeld <re************************@wolke7.net> writes: Peter Otten wrote:
int and str have no __dict__, neither in the class nor instance, and hence no place to store your additions:
> "__dict__" in dir(4) False> "__dict__" in dir(4 .__class__) False
So why does "print int.__dict__" produce a value? "__dict__" in dir(4 .__class__.__class__)
True
:-)
Cheers,
mwh
--
If Unicode is a horde of zombies with flaming dung sticks,
the hideous intricacies of JIS, Chinese Big-5, Chinese
Traditional, KOI-8, et cetera are at least an army of ogres
with salt and flensing knives. -- Eric S. Raymond, python-dev This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: David MacQuigg |
last post by:
I'm not getting any feedback on the most important benefit in my
proposed "Ideas for Python 3" thread - the unification of methods and
functions. Perhaps it was buried among too many other less...
|
by: Neil Zanella |
last post by:
Hello,
I would like to know whether it is possible to define static class methods
and data members in Python (similar to the way it can be done in C++ or Java).
These do not seem to be mentioned...
|
by: Noam Raphael |
last post by:
Hello,
I thought about a new Python feature. Please tell me what you think
about it.
Say you want to write a base class with some unimplemented methods, that
subclasses must implement (or...
|
by: John M. Gabriele |
last post by:
I've done some C++ and Java in the past, and have recently learned
a fair amount of Python. One thing I still really don't get though
is the difference between class methods and instance methods. I...
|
by: Adrian Herscu |
last post by:
Hi all,
In which circumstances it is appropriate to declare methods as non-virtual?
Thanx,
Adrian.
| |
by: Mark |
last post by:
I have it in my head that I saw a marketing document from Microsoft with the
total number of classes and methods in the .NET Framework. Something like
70,000 methods? I don't need precise numbers,...
|
by: Genboy |
last post by:
My "VIS" Website, which is a C# site created in VS.NET, Framework 1.1,
is no longer compiling for me via the command line.
As I have done 600 times in the last year and a half, I can compile to...
|
by: rickeringill |
last post by:
Hi comp.lang.javascript,
I'm throwing this in for discussion. First up I don't claim to be any
sort of authority on the ecmascript language spec - in fact I'm a
relative newb to these more...
|
by: Cliff Williams |
last post by:
Can someone explain the pros/cons of these different ways of creating a
class?
// 1
function myclass() {
this.foo1 = function() {...}
}
// 2a
|
by: fgh.vbn.rty |
last post by:
Hi,
I'm not sure if i'm asking the question correctly but anyway here it
is.
Say I have 3 classes - class A, class B, class R.
1) A and B are the building blocks and R is like a repository...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
| |
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |