J
Jon Clements
Hi All,
I've reached the point in using Python where projects, instead of being
like 'batch scripts', are becoming more like 'proper' programs.
Therefore, I'm re-designing most of these and have found things in
common which I can use classes for. As I'm only just starting to get
into classes, I see that new style classes are thte way to go, so will
be using those. I come from a C++ background, and understand I need to
adjust my thinking in certain ways - I have read
http://www.geocities.com/foetsch/python/new_style_classes.htm.
As a really simple class, I've decided to make a 'str' to include a
'substr' function. Yes, I know this can be done using slicing, and
effectively this is what substr would do: something like;
class mystr(str):
"""" My rather rubbish but trying to be simple custom string class
"""
def substr(self,start,length,pad=False):
"""
Return str of (up to) _length_ chars, starting at _start_ which
is 1 offset based.
If pad is True, ensure _length_ chars is returned by padding
with trailing whitespace.
""""
return self.<what>[ (start-1): (start-1)+length ]
Ignore the fact pad isn't implemented...
<what> should be the actual string value of the string object: How do I
work out what this is?
Secondly, I'm not 100% sure what I need for the __init__; is str's
__init__ implicitly called, or do I need to call str's __init__ in
mystr's (I seem to remember seeing some code which did this, as well as
calling super()).
Any critiscm is appreciated.
Many thanks,
Jon.
I've reached the point in using Python where projects, instead of being
like 'batch scripts', are becoming more like 'proper' programs.
Therefore, I'm re-designing most of these and have found things in
common which I can use classes for. As I'm only just starting to get
into classes, I see that new style classes are thte way to go, so will
be using those. I come from a C++ background, and understand I need to
adjust my thinking in certain ways - I have read
http://www.geocities.com/foetsch/python/new_style_classes.htm.
As a really simple class, I've decided to make a 'str' to include a
'substr' function. Yes, I know this can be done using slicing, and
effectively this is what substr would do: something like;
class mystr(str):
"""" My rather rubbish but trying to be simple custom string class
"""
def substr(self,start,length,pad=False):
"""
Return str of (up to) _length_ chars, starting at _start_ which
is 1 offset based.
If pad is True, ensure _length_ chars is returned by padding
with trailing whitespace.
""""
return self.<what>[ (start-1): (start-1)+length ]
Ignore the fact pad isn't implemented...
<what> should be the actual string value of the string object: How do I
work out what this is?
Secondly, I'm not 100% sure what I need for the __init__; is str's
__init__ implicitly called, or do I need to call str's __init__ in
mystr's (I seem to remember seeing some code which did this, as well as
calling super()).
Any critiscm is appreciated.
Many thanks,
Jon.