string methods of a str subclass

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Daniel Nogradi

    string methods of a str subclass

    I am probably misunderstandin g some basic issue here but this
    behaviour is not what I would expect:



    Python 2.4 (#1, Mar 22 2005, 21:42:42)
    [GCC 3.3.5 20050117 (prerelease) (SUSE Linux)] on linux2
    Type "help", "copyright" , "credits" or "license" for more information.
    >>class mystr( str ):
    .... pass
    ....
    >>x = mystr( 'x' )
    >>isinstance( x, mystr )
    True
    >>isinstance( x.strip( ), mystr )
    False
    >>>

    Why is the strip( ) method returning something that is not a mystr
    instance? I would expect all methods operating on a string instance
    and returning another string instance to correctly operate on a mystr
    instance and return a mystr instance. How would I achieve something
    like this without manually copying all string returning methods from
    str and stuffing the result to mystr( ) before returning?
  • 7stud

    #2
    Re: string methods of a str subclass

    On Apr 16, 3:28 am, "Daniel Nogradi" <nogr...@gmail. comwrote:
    I am probably misunderstandin g some basic issue here but this
    behaviour is not what I would expect:
    >
    Python 2.4 (#1, Mar 22 2005, 21:42:42)
    [GCC 3.3.5 20050117 (prerelease) (SUSE Linux)] on linux2
    Type "help", "copyright" , "credits" or "license" for more information.>>c lass mystr( str ):
    >
    ... pass
    ...
    >
    >x = mystr( 'x' )
    >isinstance( x, mystr )
    True
    >isinstance( x.strip( ), mystr )
    False
    >
    Why is the strip( ) method returning something that is not a mystr
    instance? I would expect all methods operating on a string instance
    and returning another string instance to correctly operate on a mystr
    instance and return a mystr instance. How would I achieve something
    like this without manually copying all string returning methods from
    str and stuffing the result to mystr( ) before returning?
    class A(object):
    def __init__(self, s):
    self.s = s
    def strip(self):
    return 2

    class mystr(A):
    pass

    x = mystr("x")
    print isinstance(x, mystr)
    print isinstance(x.st rip(), mystr)


    Comment

    • Duncan Booth

      #3
      Re: string methods of a str subclass

      "Daniel Nogradi" <nogradi@gmail. comwrote:
      Why is the strip( ) method returning something that is not a mystr
      instance? I would expect all methods operating on a string instance
      and returning another string instance to correctly operate on a mystr
      instance and return a mystr instance.
      Why would you expect that?
      Would you expect the __str__ and__repr__ methods also to return a mystr
      instance? If not those, then which other ones might also be excluded?
      Is x.encode('zip') still a mystr instance or an encoded byte-string?
      How would I achieve something
      like this without manually copying all string returning methods from
      str and stuffing the result to mystr( ) before returning?
      You don't without wrapping all the affected methods. It doesn't need to
      involve manual copying though: you have a programming language available so
      just write a list of method names and then some code to wrap them
      automatically.

      Comment

      • 7stud

        #4
        Re: string methods of a str subclass

        On Apr 16, 3:28 am, "Daniel Nogradi" <nogr...@gmail. comwrote:
        I would expect all methods operating on a string instance
        and returning another string instance
        Ok, then this:

        class A(object):
        def __init__(self, s):
        self.s = s
        def strip(self):
        return self.s

        class mystr(A):
        pass

        x = mystr("x")
        print isinstance(x, mystr)
        print isinstance(x.st rip(), mystr)


        "x" is a string, and that is what gets passed to the base class's
        __init__ method, and that is what strip() operates on.


        Comment

        • Daniel Nogradi

          #5
          Re: string methods of a str subclass

          Why is the strip( ) method returning something that is not a mystr
          instance? I would expect all methods operating on a string instance
          and returning another string instance to correctly operate on a mystr
          instance and return a mystr instance.
          >
          Why would you expect that?
          Would you expect the __str__ and__repr__ methods also to return a mystr
          instance? If not those, then which other ones might also be excluded?
          Is x.encode('zip') still a mystr instance or an encoded byte-string?
          Okay, good point, thanks.
          How would I achieve something
          like this without manually copying all string returning methods from
          str and stuffing the result to mystr( ) before returning?
          >
          You don't without wrapping all the affected methods. It doesn't need to
          involve manual copying though: you have a programming language available so
          just write a list of method names and then some code to wrap them
          automatically.
          Yes, this is in fact what I am doing, using __getattr__ and such. Thanks again.

          Comment

          Working...