python - How to bestow string-ness on my class? -
I want a string with an additional attribute, let's say it's printed in red or green.
Subclass (SRR) does not work, because it is irreversible I see value, but it can be annoying.
What many heirs can help? I have never used it.
Just entering the object and using self.value = str means that I have to apply all string-ness messages (like Strip) manually.
Or is there any way to move them, like Ruby's missing_pati?
I think a class level dictionary could be used to store colors for example. Unless you are using the old Python version, for example:
/ P>
& gt; & Gt; & Gt; Class A (str): ... def __new__ (cls, color, * args, ** kwargs): ... newobj = str .__ new __ (CLS, * args, ** kwargs) ... newobj.color = Color ... return newobj & gt; & Gt; & Gt; A = A ("#FFF", "horse") & gt; & Gt; & Gt; A.color '#fff' & gt; & Gt; & Gt; A 'horse' & gt; & Gt; & Gt; A.startswith ("h") is true
Comments
Post a Comment