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

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

    #16
    Re: Possible constant assignment operators ":=&quo t; and "::=&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]

    But it makes no sense to use a mutable object for a constant!
    The user should use a tuple, or a custom list-like type where
    all methods with side effects are removed, so it effectively acts
    as a tuple.

    Michele Simionato

    Comment

    • Edward Elliott

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

      Michele Simionato wrote:[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]
      >
      > But it makes no sense to use a mutable object for a constant!
      > The user should use a tuple,[/color]

      Sure. Now show me the builtin immutable equivalent of a dict.
      [color=blue]
      > or a custom list-like type where
      > all methods with side effects are removed, so it effectively acts
      > as a tuple.[/color]

      Ugh, not worth the trouble imo.


      Comment

      • Bruno Desthuilliers

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

        tsaar2003@yahoo .com a écrit :[color=blue]
        > Bruno Desthuilliers wrote:
        >[color=green]
        >>tsaar2003@yah oo.com a écrit :
        >>[color=darkred]
        >>>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=green][color=darkred]
        >>>>A = [] # let's declare a "constant" here[/color][/color][/color]

        Uh ? That's the strangest idea I've ever seen - I mean, using an empty
        list as a constant... If you need your constant to be a sequence (while
        I can't imagine any reason to do so), use a tuple.

        (snip)
        [color=blue]
        > 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,[/color]

        Don't worry about the symbol, use an immutable type !-)

        Comment

        • Bruno Desthuilliers

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

          tsaar2003@yahoo .com a écrit :[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.
          >
          > 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[/color]

          def A():
          return ['1']

          b = A()
          b.append('2')

          Comment

          • Michele Simionato

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

            Edward Elliott wrote:[color=blue]
            > Michele Simionato wrote:[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]
            > >
            > > But it makes no sense to use a mutable object for a constant!
            > > The user should use a tuple,[/color]
            >
            > Sure. Now show me the builtin immutable equivalent of a dict.[/color]

            There is none. However it is pretty easy to get one:

            import UserDict

            class ReadOnlyDict(Us erDict.DictMixi n):
            def __init__(self, dic):
            self._dic = dic
            def __getitem__(sel f, name):
            return self._dic[name]
            def keys(self):
            return self._dic.keys( )
            def __getitem__(sel f, name):
            raise TypeError('this dictionary is read-only')
            def __delitem__(sel f, name):
            raise TypeError('this dictionary is read-only')

            I am sure you already know that, this snipped is for the benefit of the
            other
            readers of this thread. Of course, the ReadOnlyDict can be modified by
            modifying ._dic, but the point was all about avoiding accidental
            modifications.
            [color=blue][color=green]
            > > or a custom list-like type where
            > > all methods with side effects are removed, so it effectively acts
            > > as a tuple.[/color]
            >
            > Ugh, not worth the trouble imo.[/color]

            Agreed, use a tuple if you need a tuple.

            Michele Simionato

            Comment

            • Fredrik Lundh

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

              Christophe wrote:
              [color=blue]
              > I think you've made a mistake in your example.[/color]
              [color=blue][color=green][color=darkred]
              >>> constant A = []
              >>> def foo(var):[/color][/color][/color]
              ... var.append('1')
              ... print var
              ...[color=blue][color=green][color=darkred]
              >>> b = A
              >>> foo(b)
              >>> foo(b)[/color][/color][/color]
              [color=blue][color=green]
              > > and this ?
              > >[color=darkred]
              > > >>> constant A = []
              > > >>> print A is A[/color][/color]
              >
              > Obviously, False.[/color]

              why obviously ? why shouldn't a constant be constant ?

              </F>



              Comment

              • Christophe

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

                Fredrik Lundh a écrit :[color=blue]
                > Christophe wrote:
                >
                >[color=green]
                >>I think you've made a mistake in your example.[/color]
                >
                >[color=green][color=darkred]
                > >>> constant A = []
                > >>> def foo(var):[/color][/color]
                > ... var.append('1')
                > ... print var
                > ...[color=green][color=darkred]
                > >>> b = A
                > >>> foo(b)
                > >>> foo(b)[/color][/color]
                >
                >[color=green][color=darkred]
                >>>and this ?
                >>>
                >>> >>> constant A = []
                >>> >>> print A is A[/color]
                >>
                >>Obviously, False.[/color]
                >
                >
                > why obviously ? why shouldn't a constant be constant ?
                >
                > </F>[/color]

                Because the name A is bound by the compiler to mean construct a new
                object since [] is mutable.

                Same reason why :
                [color=blue][color=green][color=darkred]
                >>> def A(): return []
                >>> print A() is A()[/color][/color][/color]
                False

                It gives btw one possible implementation ;)

                Comment

                • Piet van Oostrum

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

                  >>>>> tsaar2003@yahoo .com (T) wrote:
                  [color=blue]
                  >T> As you can see, the "constant" A can be modified this easily. But if
                  >T> there were an intuitive mechanism to declare a symbol to be immutable,
                  >T> then there won't be this problem.[/color]

                  Mutability is not a property of symbols but of values. So it doesn't make
                  sense to declare an identifier to be immutable. And mutability is tied to
                  the object's type, not to individual instances.

                  What you want can only be obtained if Python would have an immutable
                  variant for each type or would add an immutable attribute to each object.
                  --
                  Piet van Oostrum <piet@cs.uu.n l>
                  URL: http://www.cs.uu.nl/~piet [PGP 8DAE142BE17999C 4]
                  Private email: piet@vanoostrum .org

                  Comment

                  • Edward Elliott

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

                    Piet van Oostrum wrote:
                    [color=blue][color=green][color=darkred]
                    >>>>>> tsaar2003@yahoo .com (T) wrote:[/color][/color]
                    >[color=green]
                    >>T> As you can see, the "constant" A can be modified this easily. But if
                    >>T> there were an intuitive mechanism to declare a symbol to be immutable,
                    >>T> then there won't be this problem.[/color]
                    >
                    > Mutability is not a property of symbols but of values. So it doesn't make
                    > sense to declare an identifier to be immutable. And mutability is tied to
                    > the object's type, not to individual instances.[/color]

                    I think he meant immutable binding, not immutable symbol. So
                    rebinding/overshadowing a "constant" A would raise an error, but mutating
                    the underlying object A refers to would not (unless it too were immutable).
                    As far objects themselves, adding an ability to make any object immutable
                    regardless of type is exactly what he suggests.

                    --
                    Edward Elliott
                    UC Berkeley School of Law (Boalt Hall)
                    complangpython at eddeye dot net

                    Comment

                    • Piet van Oostrum

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

                      >>>>> Edward Elliott <nobody@127.0.0 .1> (EE) wrote:
                      [color=blue]
                      >EE> Piet van Oostrum wrote:[color=green][color=darkred]
                      >>>>>>>> tsaar2003@yahoo .com (T) wrote:
                      >>>[/color][/color]
                      >T> As you can see, the "constant" A can be modified this easily. But if
                      >T> there were an intuitive mechanism to declare a symbol to be immutable,
                      >T> then there won't be this problem.[color=green][color=darkred]
                      >>>
                      >>> Mutability is not a property of symbols but of values. So it doesn't make
                      >>> sense to declare an identifier to be immutable. And mutability is tied to
                      >>> the object's type, not to individual instances.[/color][/color][/color]
                      [color=blue]
                      >EE> I think he meant immutable binding, not immutable symbol. So
                      >EE> rebinding/overshadowing a "constant" A would raise an error, but
                      >EE> mutating the underlying object A refers to would not (unless it too
                      >EE> were immutable).[/color]

                      The way I understood it was that he meant both.
                      --
                      Piet van Oostrum <piet@cs.uu.n l>
                      URL: http://www.cs.uu.nl/~piet [PGP 8DAE142BE17999C 4]
                      Private email: piet@vanoostrum .org

                      Comment

                      • Steve Holden

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

                        Piet van Oostrum wrote:[color=blue][color=green][color=darkred]
                        >>>>>>Edward Elliott <nobody@127.0.0 .1> (EE) wrote:[/color][/color]
                        >
                        >[color=green]
                        >>EE> Piet van Oostrum wrote:
                        >>[color=darkred]
                        >>>>>>>>>tsaar2 003@yahoo.com (T) wrote:
                        >>>>[/color]
                        >>T> As you can see, the "constant" A can be modified this easily. But if
                        >>T> there were an intuitive mechanism to declare a symbol to be immutable,
                        >>T> then there won't be this problem.
                        >>[color=darkred]
                        >>>>Mutabilit y is not a property of symbols but of values. So it doesn't make
                        >>>>sense to declare an identifier to be immutable. And mutability is tied to
                        >>>>the object's type, not to individual instances.[/color][/color]
                        >
                        >[color=green]
                        >>EE> I think he meant immutable binding, not immutable symbol. So
                        >>EE> rebinding/overshadowing a "constant" A would raise an error, but
                        >>EE> mutating the underlying object A refers to would not (unless it too
                        >>EE> were immutable).[/color]
                        >
                        >
                        > The way I understood it was that he meant both.[/color]

                        The way I understood it was that he didn't understand it. But then he
                        was gracious enough to admit that under questioning.

                        regards
                        Steve
                        --
                        Steve Holden +44 150 684 7255 +1 800 494 3119
                        Holden Web LLC/Ltd http://www.holdenweb.com
                        Love me, love my blog http://holdenweb.blogspot.com
                        Recent Ramblings http://del.icio.us/steve.holden

                        Comment

                        Working...