Need help to pass self.count to other classes.

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Fri Jan 8 05:56:18 CET 2010


On Wed, 06 Jan 2010 08:56:23 -0500, Steve Holden wrote:

> This is untested code (some days I don't seem to write any other kind
> ...) but it should give you the flavor:
> 
> class kbInterface(object):
>     def __init__(self):
>         self.zxc = 0
>     def prompt1(self):
>         self.count += 1
>         return "[%d]> "
>     def prompt2(self):
>         l = len(str(self.count))+1
>         return "%s " % "."*l
>     def dhook(self, value):
>         print "[%d out]" % self.count
>     def ehook(self, type, value, trace):
>         print "[%d err]\n" % value
> 
> kbi = kbInterface()
> sys.ps1 = kbi.prompt1
> sys.ps2 = kbi.prompt2
> sys.displayhook = kbi.dhook
> sys.excepthook = kbi.ehook


Unfortunately this won't do what you expect, because sys.ps1 and ps2 
should be either strings, or objects with a __str__ method. They aren't 
called to generate the prompt.

(After fixing the typo with self.count vs self.zxc)


>>> kbi = kbInterface()
>>> sys.ps1 = kbi.prompt1
<bound method kbInterface.prompt1 of <__main__.kbInterface object at 
0xb7cbd52c>>print "Hello"
Hello
<bound method kbInterface.prompt1 of <__main__.kbInterface object at 
0xb7cbd52c>>



-- 
Steven



More information about the Python-list mailing list