Inheriting str object

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • kungfoobar@gmail.com

    #1

    Inheriting str object

    I want to have a str with custom methods, but I have this problem:

    class myStr(str):
    def hello(self):
    return 'hello '+self

    s=myStr('world' )
    print s.hello() # prints 'hello world'
    s=s.upper()
    print s.hello() # expected to print 'hello WORLD', but s is no longer
    myStr, it's a regular str!

    What can I do?

  • Marc 'BlackJack' Rintsch

    #2
    Re: Inheriting str object

    In <1170672488.530 515.181340@k78g 2000cwa.googleg roups.com>,
    kungfoobar@gmai l.com wrote:
    I want to have a str with custom methods, but I have this problem:
    >
    class myStr(str):
    def hello(self):
    return 'hello '+self
    >
    s=myStr('world' )
    print s.hello() # prints 'hello world'
    s=s.upper()
    print s.hello() # expected to print 'hello WORLD', but s is no longer
    myStr, it's a regular str!
    >
    What can I do?
    Return a `myStr` instance instead of a regular `str`:

    def hello(self):
    return myStr('hello ' + self)

    Ciao,
    Marc 'BlackJack' Rintsch

    Comment

    • Benjamin Niemann

      #3
      Re: Inheriting str object

      Marc 'BlackJack' Rintsch wrote:
      In <1170672488.530 515.181340@k78g 2000cwa.googleg roups.com>,
      kungfoobar@gmai l.com wrote:
      >
      >I want to have a str with custom methods, but I have this problem:
      >>
      >class myStr(str):
      > def hello(self):
      > return 'hello '+self
      >>
      >s=myStr('world ')
      >print s.hello() # prints 'hello world'
      >s=s.upper()
      >print s.hello() # expected to print 'hello WORLD', but s is no longer
      >myStr, it's a regular str!
      >>
      >What can I do?
      >
      Return a `myStr` instance instead of a regular `str`:
      >
      def hello(self):
      return myStr('hello ' + self)
      yes, but the 'upper' method is the problem here.
      So you'd have to override all string methods, like

      class myStr(str):
      ...

      def upper(self):
      return myStr(str.upper (self))


      And I'm not sure, if it then works in the intended way...
      What you are probably looking for, is to extend the 'str' class itself, so
      every str instance has your added functionality.
      Don't know, if this is possible at all, but usually it's not a good idea to
      mess with the bowels of Python unless you have some greater surgical
      skills.


      HTH

      --
      Benjamin Niemann
      Email: pink at odahoda dot de
      WWW: http://pink.odahoda.de/

      Comment

      • Virgil Dupras

        #4
        Re: Inheriting str object

        On Feb 5, 5:48 am, "kungfoo...@gma il.com" <kungfoo...@gma il.com>
        wrote:
        I want to have a str with custom methods, but I have this problem:
        >
        class myStr(str):
        def hello(self):
        return 'hello '+self
        >
        s=myStr('world' )
        print s.hello() # prints 'hello world'
        s=s.upper()
        print s.hello() # expected to print 'hello WORLD', but s is no longer
        myStr, it's a regular str!
        >
        What can I do?
        To prevent operations with your myStr class to return a simple str
        instance, you will have to override pretty much all str method, as
        well as magic methods, such as __add__, __radd__ etc.. Major pain in
        perspective. You might want to reconsider you developing strategy.

        Comment

        • =?ISO-8859-1?Q?BJ=F6rn_Lindqvist?=

          #5
          Re: Inheriting str object

          On 5 Feb 2007 02:48:08 -0800, kungfoobar@gmai l.com <kungfoobar@gma il.comwrote:
          I want to have a str with custom methods, but I have this problem:
          >
          class myStr(str):
          def hello(self):
          return 'hello '+self
          >
          s=myStr('world' )
          print s.hello() # prints 'hello world'
          s=s.upper()
          print s.hello() # expected to print 'hello WORLD', but s is no longer
          myStr, it's a regular str!
          You could use the proxy pattern:

          class GreeterString(o bject):
          def __init__(self, str):
          self.proxy = str

          def hello(self):
          return 'hello ' + self.proxy

          def __getattr__(sel f, attr):
          if attr in dir(self.proxy) :
          proxy_attr = getattr(self.pr oxy, attr)
          if callable(proxy_ attr):
          def wrapper(*args, **kwargs):
          return self.__class__( proxy_attr())
          return wrapper

          def __str__(self):
          return self.proxy.__st r__()


          gs = GreeterString(' world')
          print gs.upper().hell o()

          Magic methods has to be overridden manually, I think.

          --
          mvh Björn

          Comment

          • Alejandro Barroso

            #6
            Re: Inheriting str object

            On 5 feb, 11:48, "kungfoo...@gma il.com" <kungfoo...@gma il.comwrote:
            I want to have a str with custom methods, but I have this problem:
            >
            class myStr(str):
            def hello(self):
            return 'hello '+self
            >
            s=myStr('world' )
            print s.hello() # prints 'hello world'
            s=s.upper()
            print s.hello() # expected to print 'hello WORLD', but s is no longer
            myStr, it's a regular str!
            >
            What can I do?
            I'm new to this list(this is my first message) and to Python also (I'm
            learning these days), so i'm afraid that this is not what you are
            asking for but anyway

            you can create an myStr object on the fly, something like:
            s=myStr(s.upper ())

            Alex

            Comment

            Working...