losing subclass behavior

Ian McMeans imcmeans at telus.net
Thu Feb 20 07:54:32 EST 2003


I want to subclass string (into a URL type). However, I want to be able to
use a URL wherever I can use a string. I want the URL to remain a URL, even
after string operations.

Right now, I've got a bit of a problem. When I do it like this:
class URL(string):
    ... # stuff here

and then do:
url = URL("blah")
url = re.sub('#.*', '', url)

then url is no longer a URL (it is now a string).

Perhaps this is a design issue, and there's a better way to solve this
problem. What makes my URLs better than strings is that they have extra
methods, and extra data members.

Maybe I should be using aggregation instead of inheritance, and have a
URL.url member?

Ideally, I would be able to write something like a URL.__assign__ operator,
so that when I write 'URL = "blahfoo" ', what really happens is that my
__assign__ function gets called and I get to do what I want without my URL
object getting clobbered.

So is there anything like this? Like properties, but for whole objects
instead of their members? I wasn't able to find anything like it. Like I
said, maybe this is a design issue...






More information about the Python-list mailing list