B
Bill
After a year with Python 2.5 on my Windows box, I still have trouble
understanding classes.
Below, see the batch file and the configuration script for
my Python interactive prompt.
The widths of the secondary prompts increase when the self.count of
SysPrompt1 exceeds 99.
I am using a global variable "zxc" to share self.count, which is not
Pythonic.
How can I pass in self.count without a global?
I did RTFM, aka Google, but to no avail.
echo py.bat
set PYTHONSTARTUP=c:\scripts\startup.py
python
^Z
# startup.py
# inspired by:
# http://www.doughellmann.com/PyMOTW/sys/interpreter.html
import sys
class SysPrompt1(object):
def __init__(self):
self.count = 0
def __str__(self):
self.count += 1
global zxc
zxc = self.count
return '[%2d]> ' % self.count
class SysPrompt2(object):
def __str__(self):
global zxc
if zxc > 99: return '...... '
else: return '..... '
class DisplayHook(object):
def __call__(self, value):
if value is None: return
global zxc
if zxc > 99: print '[ out]', value, '\n'
else: print '[out]', value, '\n'
class ExceptHook(object):
def __call__(self, type, value, trace):
global zxc
if zxc > 99: print '[ err]', value, '\n'
else: print '[err]', value, '\n'
sys.ps1 = SysPrompt1()
sys.ps2 = SysPrompt2()
sys.displayhook = DisplayHook()
sys.excepthook = ExceptHook()
understanding classes.
Below, see the batch file and the configuration script for
my Python interactive prompt.
The widths of the secondary prompts increase when the self.count of
SysPrompt1 exceeds 99.
I am using a global variable "zxc" to share self.count, which is not
Pythonic.
How can I pass in self.count without a global?
I did RTFM, aka Google, but to no avail.
echo py.bat
set PYTHONSTARTUP=c:\scripts\startup.py
python
^Z
# startup.py
# inspired by:
# http://www.doughellmann.com/PyMOTW/sys/interpreter.html
import sys
class SysPrompt1(object):
def __init__(self):
self.count = 0
def __str__(self):
self.count += 1
global zxc
zxc = self.count
return '[%2d]> ' % self.count
class SysPrompt2(object):
def __str__(self):
global zxc
if zxc > 99: return '...... '
else: return '..... '
class DisplayHook(object):
def __call__(self, value):
if value is None: return
global zxc
if zxc > 99: print '[ out]', value, '\n'
else: print '[out]', value, '\n'
class ExceptHook(object):
def __call__(self, type, value, trace):
global zxc
if zxc > 99: print '[ err]', value, '\n'
else: print '[err]', value, '\n'
sys.ps1 = SysPrompt1()
sys.ps2 = SysPrompt2()
sys.displayhook = DisplayHook()
sys.excepthook = ExceptHook()