[addendum]
Well, actually, the previous Seq didn't solve all problems. Obvious case of nested lists. Below a modified version.
denis

class Seq(list):
        ''' specialized sequence type with improved str
                Override list's behaviour that str(list) calls repr instead of 
str on items:
                        print 2.2, [2.2], seq([2.2]) -->
                        2.2 [2.2000000000000002] [2.2]
                '''
        def __str__(self):
                if len(self) == 0:
                        return '[]'
                text = str(self[0])
                for item in self[1:]:
                        if isinstance(item, list):
                                item = Seq(item)
                        # will now call str on nested Seqs
                        text += " ,%s" %item
                return "[%s]" %text

spir a écrit :
Below an illustration of what troubles me a bit.
denis

class Seq(list):
    ''' specialized sequence type with improved str
    Override list's behaviour that list.__str__
    calls __repr__ instead of __str__ on items.
    ???
    '''
    def __str__(self):
        if len(self) == 0:
            return '[]'
        text = str(self[0])
        for item in self[1:]:
            text += " ,%s" %item
        return "[%s]" %text

print 1.1, repr(1.1)
print [1.1], Seq([1.1])
==>
1.1 1.1000000000000001
[1.1000000000000001] [1.1]

# By the way, I do not understand at all the behaviour of repr on rounded floats:
x = round(1.1,1)
print x, repr(x), "%s" %x
1.1 1.1000000000000001 1.1

???
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor




_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to