assignments - want a PEP

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

    #1

    assignments - want a PEP

    >From time to time I still use my old Mathematica system. The
    Mathematica language has some nice properties. The one I like best is
    the possibility to create symbols from nothing. Translated into the
    Python realm following creations are possible:
    [color=blue][color=green][color=darkred]
    >>> a[/color][/color][/color]
    a

    That's it. Just make an 'a' as a pure symbol. The reason for those
    symbols is late evaluation. Most of the time an expression should be
    transformed and simplified before it is finally evaluated using
    concrete numbers or other stuff. I tried to mimic this in Python using
    a Symbol() class:
    [color=blue][color=green][color=darkred]
    >>> a = Symbol()
    >>> a[/color][/color][/color]
    a

    So 'a' would be a Symbol object that is represented by the name "a" not
    by the standard format information that tells us that a is some member
    of class Symbol(). Starting with Symbol() subclsses of Symbol() e.g
    Expr() could overload operators like __add__, __mul__ etc. in order to
    create new symbolic objects:
    [color=blue][color=green][color=darkred]
    >>> a,b = Expr(),Expr()
    >>> (a+b)/c[/color][/color][/color]
    a+b
    ---
    c

    The availability of Symbol() would make it easy to create a Computer
    Algebra System ( CAS ) in Python, just using standard Python operators.

    Unfortunately it is not possible to implement Symbol() in an elegant
    way. Symbol() has to retrieve somehow it's own name from the
    environment where the name is created, but no assignment/name binding
    event is accessible to Python objects.

    I want to propose making name binding accessible by introducing an
    __assign__ method which is called, whenever an object is bound to a
    name. By default the __assign__ method does nothing at all but can be
    implemented by custom classes.

    def __assign__(self ,own_name):
    # do somtehing with me and my name

    Instead of changing the behaviour of the current assignment operator
    "=" one could think about introduction of a new assignment ":=" that is
    connected to the __assign__ method of custom classes if available.

    What do You think?

    Regards,
    Kay

  • Paul Rubin

    #2
    Re: assignments - want a PEP

    "Kay Schluehr" <kay.schluehr@g mx.net> writes:[color=blue][color=green][color=darkred]
    > >>> a = Symbol()
    > >>> a[/color][/color]
    > a[/color]

    Use

    a = Symbol('a')

    instead and it should solve most of the problems you mention. What's
    supposed to happen anyway, in your proposal, after

    a = Symbol()
    b = a
    print b

    ?

    Comment

    • Bengt Richter

      #3
      Re: assignments - want a PEP

      On 31 Mar 2005 14:48:00 -0800, "Kay Schluehr" <kay.schluehr@g mx.net> wrote:
      [color=blue][color=green]
      >>From time to time I still use my old Mathematica system. The[/color]
      >Mathematica language has some nice properties. The one I like best is
      >the possibility to create symbols from nothing. Translated into the
      >Python realm following creations are possible:
      >[color=green][color=darkred]
      >>>> a[/color][/color]
      >a
      >
      >That's it. Just make an 'a' as a pure symbol. The reason for those
      >symbols is late evaluation. Most of the time an expression should be
      >transformed and simplified before it is finally evaluated using
      >concrete numbers or other stuff. I tried to mimic this in Python using
      >a Symbol() class:
      >[color=green][color=darkred]
      >>>> a = Symbol()
      >>>> a[/color][/color]
      >a
      >
      >So 'a' would be a Symbol object that is represented by the name "a" not
      >by the standard format information that tells us that a is some member
      >of class Symbol(). Starting with Symbol() subclsses of Symbol() e.g
      >Expr() could overload operators like __add__, __mul__ etc. in order to
      >create new symbolic objects:
      >[color=green][color=darkred]
      >>>> a,b = Expr(),Expr()
      >>>> (a+b)/c[/color][/color]
      >a+b
      >---
      > c
      >
      >The availability of Symbol() would make it easy to create a Computer
      >Algebra System ( CAS ) in Python, just using standard Python operators.
      >
      >Unfortunatel y it is not possible to implement Symbol() in an elegant
      >way. Symbol() has to retrieve somehow it's own name from the
      >environment where the name is created, but no assignment/name binding
      >event is accessible to Python objects.[/color]
      If you let your symbols like a live in a CAS class instance attribute namespace,
      you can do what you like, pretty much. I.e.,

      cas = CAS()
      cas.a # like your plain a, where you said "that's it"


      cas.a, cas.b = cas.Expr(), cas.Expr()
      (cas.a + cas.b)/cas.c

      Etc.
      [color=blue]
      >
      >I want to propose making name binding accessible by introducing an
      >__assign__ method which is called, whenever an object is bound to a
      >name. By default the __assign__ method does nothing at all but can be
      >implemented by custom classes.
      >
      >def __assign__(self ,own_name):
      > # do somtehing with me and my name
      >[/color]
      [color=blue]
      >Instead of changing the behaviour of the current assignment operator
      >"=" one could think about introduction of a new assignment ":=" that is
      >connected to the __assign__ method of custom classes if available.
      >
      >What do You think?
      >[/color]
      I think Guido does not want bare names to work like properties, even though
      some of that could be interesting ;-)

      Regards,
      Bengt Richter

      Comment

      • Kay Schluehr

        #4
        Re: assignments - want a PEP


        Bengt Richter wrote:[color=blue]
        > you can do what you like, pretty much. I.e.,
        >
        > cas = CAS()
        > cas.a # like your plain a, where you said "that's it"
        >
        >
        > cas.a, cas.b = cas.Expr(), cas.Expr()
        > (cas.a + cas.b)/cas.c
        >
        > Etc.[/color]

        Hmmm... feels like a good idea if extended to use properties as special
        constructors:
        [color=blue][color=green][color=darkred]
        >>> expr = CAS(Expr)
        >>> expr.a[/color][/color][/color]
        a[color=blue][color=green][color=darkred]
        >>> type(expr.a)[/color][/color][/color]
        <type 'Expr'>
        [color=blue][color=green][color=darkred]
        >>> expr.a+expr.b[/color][/color][/color]
        a+b

        or a bit shorter if Expr becomes the "master algebra":
        [color=blue][color=green][color=darkred]
        >>> _ = CAS(Expr)
        >>> _.a + _.b[/color][/color][/color]
        a+b

        Regarding that an object oriented CAS will mix an arbitray number of
        algebras/namespaces that use algebraic operators homogenously
        referencing namespaces is a reasonable for providing clarity.

        A helpfull suggestion. Thanks Bengt!

        Regards,
        Kay


        Regards,
        Kay

        Comment

        Working...