Possible constant assignment operators ":=" and "::=" for Python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tsaar2003@yahoo.com

    #1

    Possible constant assignment operators ":=" and "::=" for Python

    Hi Pythonians,

    To begin with I'd like to apologize that I am not very experienced
    Python programmer so please forgive me if the following text does not
    make any sense.

    I have been missing constants in Python language. There are some
    workarounds available, for example the const-module. To me, this looks
    quite cumbersome and very unintuitive. For the interpreter, in the
    efficiency-wise, I just cannot tell.

    For the solution I came up with two new assignment operators to the
    language which would create a constant name and constant value: Let's
    welcome the new constant assignment operators := and ::=

    The := assignment operator says that "declare the name to be constant
    and immutable". However, there might be some side-effects and the
    constant may not always be constant as you will see below:

    a = [1, 2, 3]
    b := [4, 5, 6] # assign [4,5,6] to 'b' and declare name 'b' to
    be "constant" and "immutable"
    c := a # assign 'a' to 'c' and declare that name 'c' is
    "constant" and "immutable"
    d = c # assign 'c' to 'd' and 'd' inherits the
    "immutable"-attribute from 'c', but 'd' can be assigned
    later
    e = d # assign 'd' to 'e' and 'e' inherits the
    "immutable"-attribute from 'd', but 'e' can be assigned
    later
    a.append( b )
    print a # prints [1, 2, 3, 4, 5, 6]
    print c # prints [1, 2, 3, 4, 5, 6] because of the side-effect
    print d # prints [1, 2, 3, 4, 5, 6] as well
    print e # prints [1, 2, 3, 4, 5, 6] no magic here
    c = b # 'c' cannot be assigned as 'c' is "constant"
    c := b # 'c' cannot be redefined either as 'c' is
    "constant"
    c.append( 7 ) # cannot be done as 'c' is "immutable"
    d.append( 7 ) # cannot be done as 'd' is "immutable" as it was
    inherited from 'c'.
    e.append( 7 ) # cannot be done as 'e' is "immutable" as it was
    inherited from 'd'.
    d = [7, 8, 9] # normal variable assignment because 'd' was not
    declared to be a "constant"
    d.append( 10 ) # now allowed as 'd' is not "immutable" any more

    The ::= copy&assign-operator says that "make a copy of the
    right-hand-side object and declare the name to be constant and
    immutable ". This would give us a true constant object which cannot be
    changed. Example follows:

    a = [1, 2, 3]
    b = [4, 5, 6]
    c ::= a # make copy of 'a' and assign that new copy to 'c' and
    declare that name 'c' is a "constant" and "immutable"
    d = c # assign 'c' to 'd' and 'd' inherits the
    "immutable"-attribute from 'c', but 'd' can be assigned
    later
    e := d # assign 'd' to 'e' and declare that name 'e' is
    "constant" and "immutable"
    a.append( b )
    print a # prints [1, 2, 3, 4, 5, 6]
    print c # prints [1, 2, 3] as 'a' and 'c' are two different
    objects because of the ::=
    print d # prints [1, 2, 3] no magic here
    print e # prints [1, 2, 3] no magic here either
    c = b # 'c' cannot be assigned as 'c' is "constant"
    c := b # 'c' cannot be redefined either as 'c' is
    "constant"
    c.append( 7 ) # cannot be done as 'c' is "immutable"
    d.append( 7 ) # cannot be done as 'd' is "immutable" as it was
    inherited from 'c'.
    e.append( 7 ) # cannot be done as 'e' is "immutable" as it was
    inherited from 'd'.
    d = [7, 8, 9] # normal variable assignment because 'd' was not
    declared to be a "constant"
    d.append( 10 ) # now allowed as 'd' is not "immutable" any more

    The := operator would be computionally efficient as it creates only a
    reference to an existing object but it may suffer from side-effects.
    The ::= is less efficient as it makes a copy on an existing object, but
    gives truly constant objects.

    Does this make any sense to you, or are there some fatal issues that I
    just happened to overlook?

    Br,

    T.S.

  • Ben Sizer

    #2
    Re: Possible constant assignment operators ":=&quo t; and "::=&qu ot; for Python

    tsaar2003@yahoo .com wrote:[color=blue]
    > To begin with I'd like to apologize that I am not very experienced
    > Python programmer so please forgive me if the following text does not
    > make any sense.
    >
    > I have been missing constants in Python language. There are some
    > workarounds available, for example the const-module. To me, this looks
    > quite cumbersome and very unintuitive. For the interpreter, in the
    > efficiency-wise, I just cannot tell.[/color]

    The question is, why have you been missing them? Constants serve a
    variety of purposes in other languages, some of which might not be
    worthwhile in Python. There was a thread about 'symbols' a while back
    which covers many of the uses of constants.

    --
    Ben Sizer

    Comment

    • Bruno Desthuilliers

      #3
      Re: Possible constant assignment operators ":=&quo t; and "::=&qu ot; for Python

      tsaar2003@yahoo .com a écrit :[color=blue]
      > Hi Pythonians,
      >
      > To begin with I'd like to apologize that I am not very experienced
      > Python programmer so please forgive me if the following text does not
      > make any sense.
      >
      > I have been missing constants in Python language.[/color]

      Why so ?

      I guess you're talking about named (symbolic) constants ? If so, just
      follow the convention : a name in ALL_UPPERCASE is a constant (or at
      least will be treated as such by anyone not wanting to mess with your
      package's implementation) . No need to add extra syntax here IMHO.

      Comment

      • tsaar2003@yahoo.com

        #4
        Re: Possible constant assignment operators ":=&quo t; and "::=&qu ot; for Python

        Bruno Desthuilliers wrote:[color=blue]
        > tsaar2003@yahoo .com a écrit :[color=green]
        > > Hi Pythonians,
        > >
        > > To begin with I'd like to apologize that I am not very experienced
        > > Python programmer so please forgive me if the following text does not
        > > make any sense.
        > >
        > > I have been missing constants in Python language.[/color]
        >
        > Why so ?
        >
        > I guess you're talking about named (symbolic) constants ? If so, just
        > follow the convention : a name in ALL_UPPERCASE is a constant (or at
        > least will be treated as such by anyone not wanting to mess with your
        > package's implementation) . No need to add extra syntax here IMHO.[/color]

        Hi Bruno,

        For example:
        [color=blue][color=green][color=darkred]
        >>> A = [] # let's declare a "constant" here
        >>> b = A # and let's assign the constant here
        >>> b.append('1') # OOPS!
        >>> c = A
        >>> print A[/color][/color][/color]
        ['1'][color=blue][color=green][color=darkred]
        >>> print b[/color][/color][/color]
        ['1'][color=blue][color=green][color=darkred]
        >>> print c[/color][/color][/color]
        ['1']

        As you can see, the "constant" A can be modified this easily. But if
        there were an intuitive mechanism to declare a symbol to be immutable,
        then there won't be this problem.

        Br,

        - T.S.

        Comment

        • Fredrik Lundh

          #5
          Re: Possible constant assignment operators ":=&quo t; and "::=&qu ot; for Python

          tsaar2003@yahoo .com wrote:
          [color=blue]
          > For example:
          >[color=green][color=darkred]
          > >>> A = [] # let's declare a "constant" here
          > >>> b = A # and let's assign the constant here
          > >>> b.append('1') # OOPS!
          > >>> c = A
          > >>> print A[/color][/color]
          > ['1'][color=green][color=darkred]
          > >>> print b[/color][/color]
          > ['1'][color=green][color=darkred]
          > >>> print c[/color][/color]
          > ['1']
          >
          > As you can see, the "constant" A can be modified this easily. But if
          > there were an intuitive mechanism to declare a symbol to be immutable,
          > then there won't be this problem.[/color]

          why are you using mutable objects as constants?

          (insert obligatory "it hurts when I do this" joke here)

          btw, given a hypothetical "object that symbol points to is immutable" syntax,
          what would you expect this to do ?
          [color=blue][color=green][color=darkred]
          >>> constant A = [][/color][/color][/color]
          [color=blue][color=green][color=darkred]
          >>> b = [A][/color][/color][/color]
          [color=blue][color=green][color=darkred]
          >>> # much later
          >>> b[0].append('1')[/color][/color][/color]

          </F>



          Comment

          • tsaar2003@yahoo.com

            #6
            Re: Possible constant assignment operators &quot;:=&quo t; and &quot;::=&qu ot; for Python

            Yes, I know that "constant" A will also be modified as the b[0] points
            to A. Obviously the [] should be marked as immutable, as A is declared
            to be constant thus immutable. If somebody tries to modify this
            immutable object an error would occur.

            When I further thought about this problem with constant objects (and
            values), I run into this scenario: What if I want to use a constant
            object/value as a template (or predefined value/class) for a variable:

            constant A = ['1'] # let's declare A as immutable constant value/object
            b = A # let's assign b some default value
            b.append('2') # and let's play with b, but I wouldn't want to change A

            What I'd like to see here is that b gets a copy of A so that the
            original A won't be modified as we play with b. However, as we assign a
            constant value A to b, I wouldn't want to restrict myself from playing
            with b. Of course, I could write something like b = list(A) to get a
            copy of A assigned to b. However, in this situation I have to know the
            class name of A. But this is something that I would no like to have to
            know if we want to take modules as some kind of black boxes.

            Br,

            T.S.

            Comment

            • Fredrik Lundh

              #7
              Re: Possible constant assignment operators &quot;:=&quo t; and &quot;::=&qu ot; for Python

              tsaar2003@yahoo .com wrote:
              [color=blue]
              > Yes, I know that "constant" A will also be modified as the b[0] points
              > to A. Obviously the [] should be marked as immutable, as A is declared
              > to be constant thus immutable. If somebody tries to modify this
              > immutable object an error would occur.[/color]

              so a constant declaration doesn't only affect the namespace, it also modifies
              the type of the object ?

              are you sure you know how Python's object model work ? if you do, please
              explain your proposal in terms of what needs to be changed, rather than in
              terms of wishful thinking.

              </F>



              Comment

              • tsaar2003@yahoo.com

                #8
                Re: Possible constant assignment operators &quot;:=&quo t; and &quot;::=&qu ot; for Python

                > are you sure you know how Python's object model work ? if you do, please[color=blue]
                > explain your proposal in terms of what needs to be changed, rather than in
                > terms of wishful thinking.[/color]

                No, I do not know. As stated in my first post, I am quite newbie in
                Python and miss a simple and intuitive mechanism that would allow to
                declare something as constant and that would protect these "constant"
                objects from accidental modifications.

                T.S.

                Comment

                • Christophe

                  #9
                  Re: Possible constant assignment operators &quot;:=&quo t; and &quot;::=&qu ot; for Python

                  Fredrik Lundh a écrit :[color=blue]
                  > tsaar2003@yahoo .com wrote:
                  >
                  >[color=green]
                  >>For example:
                  >>
                  >>[color=darkred]
                  >>>>>A = [] # let's declare a "constant" here
                  >>>>>b = A # and let's assign the constant here
                  >>>>>b.append(' 1') # OOPS!
                  >>>>>c = A
                  >>>>>print A[/color]
                  >>
                  >>['1']
                  >>[color=darkred]
                  >>>>>print b[/color]
                  >>
                  >>['1']
                  >>[color=darkred]
                  >>>>>print c[/color]
                  >>
                  >>['1']
                  >>
                  >>As you can see, the "constant" A can be modified this easily. But if
                  >>there were an intuitive mechanism to declare a symbol to be immutable,
                  >>then there won't be this problem.[/color]
                  >
                  >
                  > why are you using mutable objects as constants?
                  >
                  > (insert obligatory "it hurts when I do this" joke here)
                  >
                  > btw, given a hypothetical "object that symbol points to is immutable" syntax,
                  > what would you expect this to do ?
                  >[color=green][color=darkred]
                  > >>> constant A = [][/color][/color]
                  >[color=green][color=darkred]
                  > >>> b = [A][/color][/color]
                  >[color=green][color=darkred]
                  > >>> # much later
                  > >>> b[0].append('1')[/color][/color][/color]

                  That's easy, since A is a symbolic constant know at compile time, and
                  since it's a known mutable objet, the code once compiled will be
                  equivalent to:
                  [color=blue][color=green][color=darkred]
                  >>> b = [[]][/color][/color][/color]
                  [color=blue][color=green][color=darkred]
                  >>> # much later
                  >>> b|0].append('1')[/color][/color][/color]

                  Comment

                  • Michele Simionato

                    #10
                    Re: Possible constant assignment operators &quot;:=&quo t; and &quot;::=&qu ot; for Python

                    tsaar2003@yahoo .com wrote:[color=blue]
                    > As stated in my first post, I am quite newbie in
                    > Python and miss a simple and intuitive mechanism that would allow to
                    > declare something as constant and that would protect these "constant"
                    > objects from accidental modifications.
                    >
                    > T.S.[/color]

                    Python solution is to rely on the intelligence of programmers. If they
                    see
                    an all caps name and then they try to change it without knowing what
                    they are doing,
                    then they are stupid. If you have stupid programmers there is no way
                    the
                    language can stop them for making disasters.
                    For true constants, this is the end of the story. OTOH, sometimes you
                    want
                    read-only attributes which should not be accidentally overwritten but
                    that
                    are not really constants. In this case, the solution is to use
                    properties.
                    Just google the newsgroup for "properties " and you will find many
                    examples
                    of usage.

                    Michele Simionato

                    Comment

                    • Edward Elliott

                      #11
                      Re: Possible constant assignment operators &quot;:=&quo t; and &quot;::=&qu ot; for Python

                      Michele Simionato wrote:[color=blue]
                      > Python solution is to rely on the intelligence of programmers. If they
                      > see an all caps name and then they try to change it without knowing what
                      > they are doing, then they are stupid. If you have stupid programmers there
                      > is no way the language can stop them for making disasters.[/color]

                      Which doesn't apply here, one of the OP's examples further up this thread
                      doesn't modify any ALL CAPS vars directly:
                      [color=blue][color=green][color=darkred]
                      >>> A = []  # let's declare a "constant" here
                      >>> b = A   # and let's assign the constant here
                      >>> b.append('1') # OOPS![/color][/color][/color]


                      Comment

                      • Edward Elliott

                        #12
                        Re: Possible constant assignment operators &quot;:=&quo t; and &quot;::=&qu ot; for Python

                        tsaar2003@yahoo .com wrote:[color=blue]
                        > What I'd like to see here is that b gets a copy of A so that the
                        > original A won't be modified as we play with b. However, as we assign a
                        > constant value A to b, I wouldn't want to restrict myself from playing
                        > with b.[/color]

                        If A is a list you can take a copy-slice liek this:[color=blue][color=green][color=darkred]
                        >>> b = A[:][/color][/color][/color]

                        and changes to b won't affect A. For the general case where A isn't a list,
                        you can use the copy module to make shallow or deep copies of A.

                        Source code: Lib/copy.py Assignment statements in Python do not copy objects, they create bindings between a target and an object. For collections that are mutable or contain mutable items, a copy ...


                        It's not quite as simple or as strict as declaring A constant, but it works.
                        Or you could look at properties, there's a thread in this group within the
                        last couple weeks about making constants with properties.

                        Comment

                        • Diez B. Roggisch

                          #13
                          Re: Possible constant assignment operators &quot;:=&quo t; and &quot;::=&qu ot; for Python

                          Edward Elliott wrote:
                          [color=blue]
                          > Michele Simionato wrote:[color=green]
                          >> Python solution is to rely on the intelligence of programmers. If they
                          >> see an all caps name and then they try to change it without knowing what
                          >> they are doing, then they are stupid. If you have stupid programmers
                          >> there is no way the language can stop them for making disasters.[/color]
                          >
                          > Which doesn't apply here, one of the OP's examples further up this thread
                          > doesn't modify any ALL CAPS vars directly:
                          >[color=green][color=darkred]
                          >>>> A = []  # let's declare a "constant" here
                          >>>> b = A   # and let's assign the constant here
                          >>>> b.append('1') # OOPS![/color][/color][/color]

                          That leads to the question of forbidding mutable methods. Which will require
                          const-declarations on methods that in turn are only allowed to invoke
                          const-methods themselves.

                          Bottom line is: if one wants static typing and full control over
                          construction of new pobjects when merely assigning, use C++ :)

                          diez

                          Comment

                          • Fredrik Lundh

                            #14
                            Re: Possible constant assignment operators &quot;:=&quo t; and &quot;::=&qu ot; for Python

                            Christophe wrote:
                            [color=blue]
                            > That's easy, since A is a symbolic constant know at compile time, and
                            > since it's a known mutable objet, the code once compiled will be
                            > equivalent to:
                            >[color=green][color=darkred]
                            > >>> b = [[]][/color][/color]
                            >[color=green][color=darkred]
                            > >>> # much later
                            > >>> b|0].append('1')[/color][/color][/color]

                            the OP talked about constants as names for immutable objects, not pre-
                            processor macros. but alright, using the "symbolic constant" approach,
                            what would this print ?
                            [color=blue][color=green][color=darkred]
                            >>> def foo(var):[/color][/color][/color]
                            ... var.append('1')
                            ... print var
                            ...[color=blue][color=green][color=darkred]
                            >>> b = []
                            >>> foo(b)
                            >>> foo(b)[/color][/color][/color]

                            and this ?
                            [color=blue][color=green][color=darkred]
                            >>> constant A = []
                            >>> print A is A[/color][/color][/color]

                            </F>



                            Comment

                            • Christophe

                              #15
                              Re: Possible constant assignment operators &quot;:=&quo t; and &quot;::=&qu ot; for Python

                              Fredrik Lundh a écrit :[color=blue]
                              > Christophe wrote:
                              >
                              >[color=green]
                              >>That's easy, since A is a symbolic constant know at compile time, and
                              >>since it's a known mutable objet, the code once compiled will be
                              >>equivalent to:
                              >>[color=darkred]
                              >> >>> b = [[]][/color]
                              >>[color=darkred]
                              >> >>> # much later
                              >> >>> b|0].append('1')[/color][/color]
                              >
                              >
                              > the OP talked about constants as names for immutable objects, not pre-
                              > processor macros. but alright, using the "symbolic constant" approach,
                              > what would this print ?
                              >[color=green][color=darkred]
                              > >>> def foo(var):[/color][/color]
                              > ... var.append('1')
                              > ... print var
                              > ...[color=green][color=darkred]
                              > >>> b = []
                              > >>> foo(b)
                              > >>> foo(b)[/color][/color][/color]

                              I think you've made a mistake in your example. This is valid today's
                              Python you know :) And it'll print more or less :
                              ['1']
                              ['1', '1']
                              [color=blue]
                              > and this ?
                              >[color=green][color=darkred]
                              > >>> constant A = []
                              > >>> print A is A[/color][/color][/color]

                              Obviously, False.

                              Comment

                              Working...