Printing and str()

21 views
Skip to first unread message

brombo

unread,
Sep 25, 2022, 1:00:04 PM9/25/22
to sympy
In my class I have _latex and _sympystr.  Your latex() function uses my _latex but str() does not use _sympystr I have to define __str__() in my class to print correctly.  How should I use _sympystr to return the correct string.

    def _sympystr(self,printer):
        if self.order == 0:
            return 'D{}'
        s = 'D'
        for x in self.pdiffs:
            s += '{' + printer._print(x) + '}'
            n = self.pdiffs[x]
            if n > 1:
                s += '^' + printer._print(n)
        return s

     def _latex(self,printer):
        if self.order == 0:
            return ''
        s = r'\frac{\partial'
        if self.order > 1:
            s += '^{' + printer._print(self.order) + '}'
        s += '}{'
        keys = list(self.pdiffs.keys())
        keys.sort(key=lambda x: x.sort_key())
        for key in keys:
            i = self.pdiffs[key]
            s += r'\partial ' + printer._print(key)
            if i > 1:
                s += '^{' + printer._print(i) + '}'
        s += '}'
        return s

Aaron Meurer

unread,
Sep 26, 2022, 6:11:26 PM9/26/22
to sy...@googlegroups.com
Is your class subclassing from Basic (or Expr)? If not, it won't be
inheriting the __str__ method that automatically uses sstr().

Aaron Meurer
> --
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/3681a732-62ca-4169-84ea-291b92dccef4n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages