Hi, I didn't really know how to word the title, but I was just wondering if it was possible to write a function and make it accessible as if it was any other str function. It was a simple (but useful) few lines which returned a list of all the occurrences of a substring within a string. I'd have to call it by findAll( string, substring )
, but I would like to be able to call it like the built-in find function, i.e. string.findAll( substring )
. Is this possible? Thanks!
shadwickman
159
Posting Pro in Training
Recommended Answers
Jump to PostI don't think it's possible. The normal way to do this is to create your own subclass of
str
like thisclass mystr(str): def __new__(cls, *args): return str.__new__(cls, *args) def findAll(self, other): print("Couldn't find '%s' in '%s'!" % (other, self)) if __name__ == "__main__": s = …
All 2 Replies
Gribouillis
1,391
Programming Explorer
Team Colleague
shadwickman
commented:
Great idea to create my own str class!
+2
shadwickman
159
Posting Pro in Training
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.