code_junkie

Extending builtin classes in python

Nov 12th, 2011
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.38 KB | None | 0 0
  1. >>> class X(str):
  2. ... def myMethod( self ):
  3. ... return int(self)
  4. ...
  5. >>> s=X("Hi Mom")
  6. >>> s.lower()
  7. 'hi mom'
  8. >>> s.myMethod()
  9. Traceback (most recent call last):
  10. File "<stdin>", line 1, in <module>
  11. File "<stdin>", line 3, in myMethod
  12. ValueError: invalid literal for int() with base 10: 'Hi Mom'
  13.  
  14. >>> z=X("271828")
  15. >>> z.lower()
  16. '271828'
  17. >>> z.myMethod()
  18. 271828
Add Comment
Please, Sign In to add comment