output formatting for classes

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

    #1

    output formatting for classes

    I'd like to get output formatting for my own classes that mimics the
    built-in output formatting. For example,
    [color=blue][color=green][color=darkred]
    >>> x = 4.54
    >>> print "%4.2f" % x[/color][/color][/color]
    4.54

    In other words, if I substitute a class instance for "x" above, I'd
    like to make the format string apply to an element or elements of the
    instance. Can I somehow overload the "%" operator for that? Thanks.

    On an unrelated matter, I think the complex class in Python is too
    complex, so I plan to clean it up and implement it right. (just
    kidding, folks!)

  • Schüle Daniel

    #2
    Re: output formatting for classes

    Russ wrote:[color=blue]
    > I'd like to get output formatting for my own classes that mimics the
    > built-in output formatting. For example,
    >
    >[color=green][color=darkred]
    >>>>x = 4.54
    >>>>print "%4.2f" % x[/color][/color]
    >
    > 4.54
    >
    > In other words, if I substitute a class instance for "x" above, I'd
    > like to make the format string apply to an element or elements of the
    > instance. Can I somehow overload the "%" operator for that? Thanks.
    >
    > On an unrelated matter, I think the complex class in Python is too
    > complex, so I plan to clean it up and implement it right. (just
    > kidding, folks!)[/color]

    yeah, i miss some things in complex implementation
    for example c=complex()
    c.abs = 2**0.5
    c.angle = pi/2

    should result in 1+1j :)

    or c=complex(1,1)
    print c.abs # should print 2**0.5
    print c.angle # should print pi%2

    i think one can implement it with properties

    but to your question ...
    [color=blue][color=green][color=darkred]
    >>> class X(object):[/color][/color][/color]
    .... def __float__(self) :
    .... return 1.0
    .... def __long__(self):
    .... return 10l
    .... def __int__(self):
    .... return 20
    .... def __repr__(self):
    .... return "i am"
    .... def __str__(self):
    .... return "I AM"
    .... def __complex__(sel f):
    .... return 1+1j
    ....[color=blue][color=green][color=darkred]
    >>> x=X()
    >>> int(x)[/color][/color][/color]
    20[color=blue][color=green][color=darkred]
    >>> long(x)[/color][/color][/color]
    10L[color=blue][color=green][color=darkred]
    >>> float(x)[/color][/color][/color]
    1.0[color=blue][color=green][color=darkred]
    >>> str(x)[/color][/color][/color]
    'I AM'[color=blue][color=green][color=darkred]
    >>> repr(x)[/color][/color][/color]
    'i am'[color=blue][color=green][color=darkred]
    >>> print "%s -- %r" % (x,x)[/color][/color][/color]
    I AM -- i am[color=blue][color=green][color=darkred]
    >>> complex(x)[/color][/color][/color]
    (1+1j)[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

    Comment

    • Steven D'Aprano

      #3
      Mutable complex numbers [was Re: output formatting for classes]

      On Fri, 10 Mar 2006 02:19:10 +0100, Schüle Daniel wrote:
      [color=blue]
      > yeah, i miss some things in complex implementation
      > for example c=complex()
      > c.abs = 2**0.5
      > c.angle = pi/2
      >
      > should result in 1+1j :)[/color]

      Smiley noted, but consider:

      c = complex()
      => what is the value of c here?

      c.abs = 2**0.5
      => what is c's value now?

      c.angle = pi/2
      => now c has the value 1+1j

      Objects with indeterminate values are rarely a good idea.

      A better way would be for complex numbers to take a constructor that can
      take arguments in either Cartesian or polar form. So, hypothetically, the
      following would all be equivalent:

      1+1j
      complex(1,1)
      complex(real=1, img=1)
      complex(len=2** 0.5, theta=pi/2)

      Another alternative would be a function to construct polar form complex
      numbers. It could be a plain function or a static method:

      cmath.polar(2** 0.5, pi/2) => 1+1j
      complex.polar(2 **0.5, pi/2) => 1+1j



      --
      Steven.

      Comment

      • Schüle Daniel

        #4
        Re: Mutable complex numbers [was Re: output formatting for classes]

        Steven D'Aprano wrote:[color=blue]
        > On Fri, 10 Mar 2006 02:19:10 +0100, Schüle Daniel wrote:
        >
        >[color=green]
        >>yeah, i miss some things in complex implementation
        >>for example c=complex()
        >>c.abs = 2**0.5
        >>c.angle = pi/2
        >>
        >>should result in 1+1j :)[/color]
        >
        >
        > Smiley noted, but consider:
        >
        > c = complex()
        > => what is the value of c here?[/color]

        default value is 0, for complex number that means
        real = 0, imag = 0
        is the same as
        c.abs=0, c.angle=0

        ok mathematically c.angle can be of arbitrary value
        but defaulting it to zero is very handy
        c = complex()
        c.abs = 10
        yields 10+0j

        c=complex()
        c.real = 2
        c.imag = 2
        c.abs = 50**0.5 # angle remains, length changed
        yields 5+5j
        c.angle = 0
        yields 50**0.5 + 0j
        [color=blue]
        > c.abs = 2**0.5
        > => what is c's value now?[/color]

        c.abs = 2**0.5
        c.angle = 0
        [color=blue]
        >
        > c.angle = pi/2
        > => now c has the value 1+1j
        >
        > Objects with indeterminate values are rarely a good idea.[/color]

        IMHO it's perfectly consistent with[color=blue][color=green][color=darkred]
        >>> int()[/color][/color][/color]
        0[color=blue][color=green][color=darkred]
        >>> long()[/color][/color][/color]
        0L[color=blue][color=green][color=darkred]
        >>> float()[/color][/color][/color]
        0.0[color=blue][color=green][color=darkred]
        >>>[/color][/color][/color]
        complex()[color=blue][color=green][color=darkred]
        >>> complex()[/color][/color][/color]
        0j[color=blue][color=green][color=darkred]
        >>>[/color][/color][/color]

        but extending complex with default angle=0


        [color=blue]
        > A better way would be for complex numbers to take a constructor that can
        > take arguments in either Cartesian or polar form. So, hypothetically, the
        > following would all be equivalent:
        >
        > 1+1j
        > complex(1,1)
        > complex(real=1, img=1)
        > complex(len=2** 0.5, theta=pi/2)[/color]

        ack
        but after the creation of complex number one will have to
        do all the transformations in another coord. system manually
        [color=blue]
        > Another alternative would be a function to construct polar form complex
        > numbers. It could be a plain function or a static method:
        >
        > cmath.polar(2** 0.5, pi/2) => 1+1j
        > complex.polar(2 **0.5, pi/2) => 1+1j[/color]

        maybe adding

        c=complex.from_ polar((length,a ngle))
        d=complex.to_po lar(c)
        d == (length, angle)
        True

        would be sufficient, but I would prefer the other version

        Regards

        Comment

        Working...