Reference gotcha

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

    Reference gotcha

    Hi all,
    This interested me:
    [color=blue][color=green][color=darkred]
    >>> a1 = a2 = {}
    >>> a1['blah'] = 1
    >>> a2[/color][/color][/color]
    {'blah': 1}[color=blue][color=green][color=darkred]
    >>> a1 == a2[/color][/color][/color]
    True


    Whereas ...

    [color=blue][color=green][color=darkred]
    >>> b1 = b2 = 0
    >>> b1 += 1
    >>> b2[/color][/color][/color]
    0[color=blue][color=green][color=darkred]
    >>> b1 == b2[/color][/color][/color]
    False

    So how should one create a robust alias for a numeric type?

    This bit me when I was trying to do the following in a class:
    def __init__(self, data):
    a = self.a = {}
    b = self.b = {}
    c = self.c = 0

    # and increment them here ...

    Best regards,
    Ed Schofield


  • anton muhin

    #2
    Re: Reference gotcha

    Ed Schofield wrote:
    [color=blue]
    > Hi all,
    > This interested me:
    >
    >[color=green][color=darkred]
    >>>>a1 = a2 = {}
    >>>>a1['blah'] = 1
    >>>>a2[/color][/color]
    >
    > {'blah': 1}
    >[color=green][color=darkred]
    >>>>a1 == a2[/color][/color]
    >
    > True
    >
    >
    > Whereas ...
    >
    >
    >[color=green][color=darkred]
    >>>>b1 = b2 = 0
    >>>>b1 += 1
    >>>>b2[/color][/color]
    >
    > 0
    >[color=green][color=darkred]
    >>>>b1 == b2[/color][/color]
    >
    > False
    >
    > So how should one create a robust alias for a numeric type?
    >
    > This bit me when I was trying to do the following in a class:
    > def __init__(self, data):
    > a = self.a = {}
    > b = self.b = {}
    > c = self.c = 0
    >
    > # and increment them here ...
    >
    > Best regards,
    > Ed Schofield
    >
    >[/color]

    Standard trick is to use single element list or just a small object with
    integer field.

    hth,
    anton.

    Comment

    • Paul Prescod

      #3
      Re: Reference gotcha

      anton muhin wrote:
      [color=blue]
      >
      > Standard trick is to use single element list or just a small object with
      > integer field.[/color]

      But why do that when "self." is already a small object with an integer
      field?

      Paul Prescod



      Comment

      Working...