Bizzare lst length problem

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

    #1

    Bizzare lst length problem

    Hello...hopeful ly my last question :-)

    I ave a dictionary, where each value is a class instance. I access it
    using:

    for k, v in self.panels.pan el_list.items() :
    print "Number:\t",v.n umber
    print "Level:\t",v.le vel
    print "Location:\t",v .location
    print "MOPS:\t",v.mop s
    print "List length:\t",len( v.mops)
    print "Matrix:\t",v.m atrix,"\n\n"

    The output from this would be (for a given key value):
    Number: 181
    Level: ovride+supvis
    Location: mons=4 v8.0 3rd floor
    MOPS: ['287', '288', '289', '290']
    List Length: 28
    Matrix: kng

    This is really odd...my len(v.mops) ought to return 4 (4 elements in
    the list). In fact it returns 28. looking at outputs from lots of
    records, it seems that the length is almost always 7 time too great
    (28/7=4)....but not always. This is really confusing...can anyon
    suggest what is going on?

    I've been trying to output the list elements as a string with equally
    limmited success, but the thing seems so simple I can't see where the
    prblem might lie....

    Cheers,

    Ben

  • Fredrik Lundh

    #2
    Re: Bizzare lst length problem

    Ben wrote:
    The output from this would be (for a given key value):
    Number: 181
    Level: ovride+supvis
    Location: mons=4 v8.0 3rd floor
    MOPS: ['287', '288', '289', '290']
    List Length: 28
    Matrix: kng
    >
    This is really odd...my len(v.mops) ought to return 4 (4 elements in
    the list).
    adding a

    print type(v.mops), repr(v.mops)

    debug statement might provide you with the clues you need.
    In fact it returns 28. looking at outputs from lots of
    records, it seems that the length is almost always 7 time too great
    (28/7=4)....but not always.
    >>len("['287',")
    7
    >>len(" '288',")
    7
    >>len(" '289',")
    7
    >>len(" '290']")
    7

    </F>

    Comment

    • Ben

      #3
      Re: Bizzare lst length problem

      Ah... my list is a string. That explains the len() results, but not why
      it is a string in the dirst place.

      I have a dictionary containing a number of instances of the following
      class as values:

      class panel:
      mops =[]

      def __init__(self,n umber,level,loc ation,mops,matr ix):
      self.number=num ber
      self.level=leve l
      self.location=l ocation
      self.mops=mops
      self.matrix=mat rix


      abve mops is a list, yet when I access it it is a string...



      Fredrik Lundh wrote:
      Ben wrote:
      >
      The output from this would be (for a given key value):
      Number: 181
      Level: ovride+supvis
      Location: mons=4 v8.0 3rd floor
      MOPS: ['287', '288', '289', '290']
      List Length: 28
      Matrix: kng

      This is really odd...my len(v.mops) ought to return 4 (4 elements in
      the list).
      >
      adding a
      >
      print type(v.mops), repr(v.mops)
      >
      debug statement might provide you with the clues you need.
      >
      In fact it returns 28. looking at outputs from lots of
      records, it seems that the length is almost always 7 time too great
      (28/7=4)....but not always.
      >
      >>len("['287',")
      7
      >>len(" '288',")
      7
      >>len(" '289',")
      7
      >>len(" '290']")
      7
      >
      </F>

      Comment

      • Ben

        #4
        Re: Bizzare lst length problem

        ....and when I print out the string, it is still formatted as one would
        expect a list to be:

        <type 'str'"['01', '02', '03', '04']"



        Ben wrote:
        Ah... my list is a string. That explains the len() results, but not why
        it is a string in the dirst place.
        >
        I have a dictionary containing a number of instances of the following
        class as values:
        >
        class panel:
        mops =[]
        >
        def __init__(self,n umber,level,loc ation,mops,matr ix):
        self.number=num ber
        self.level=leve l
        self.location=l ocation
        self.mops=mops
        self.matrix=mat rix
        >
        >
        abve mops is a list, yet when I access it it is a string...
        >
        >
        >
        Fredrik Lundh wrote:
        Ben wrote:
        The output from this would be (for a given key value):
        Number: 181
        Level: ovride+supvis
        Location: mons=4 v8.0 3rd floor
        MOPS: ['287', '288', '289', '290']
        List Length: 28
        Matrix: kng
        >
        This is really odd...my len(v.mops) ought to return 4 (4 elements in
        the list).
        adding a

        print type(v.mops), repr(v.mops)

        debug statement might provide you with the clues you need.
        In fact it returns 28. looking at outputs from lots of
        records, it seems that the length is almost always 7 time too great
        (28/7=4)....but not always.
        >>len("['287',")
        7
        >>len(" '288',")
        7
        >>len(" '289',")
        7
        >>len(" '290']")
        7

        </F>

        Comment

        • John Machin

          #5
          Re: Bizzare lst length problem


          Ben wrote:
          Ah... my list is a string. That explains the len() results, but not why
          it is a string in the dirst place.
          >
          I have a dictionary containing a number of instances of the following
          class as values:
          >
          class panel:
          mops =[]
          >
          def __init__(self,n umber,level,loc ation,mops,matr ix):
          self.number=num ber
          self.level=leve l
          self.location=l ocation
          self.mops=mops
          self.matrix=mat rix
          >
          >
          abve mops is a list, yet when I access it it is a string...
          >
          Well, if you are going to spare us from reading all of your code,
          you'll have to debug it yourself. The clue that Fredrik gave you is
          *not* of the use-once-and-discard variety -- when you are having
          problems with the pixies changing your lists into strings, you need to
          sprinkle prints of type(pixie_prey ) and repr(pixie_prey ) at salient
          points in your code; as first statement in that __init__ method would
          be a good start.

          Comment

          • John Machin

            #6
            Re: Bizzare lst length problem


            Ben wrote:
            Ah... my list is a string. That explains the len() results, but not why
            it is a string in the dirst place.
            >
            I have a dictionary containing a number of instances of the following
            class as values:
            >
            class panel:
            mops =[]
            >
            def __init__(self,n umber,level,loc ation,mops,matr ix):
            self.number=num ber
            self.level=leve l
            self.location=l ocation
            self.mops=mops
            self.matrix=mat rix
            >
            >
            abve mops is a list, yet when I access it it is a string...
            >
            Well, if you are going to spare us from reading all of your code,
            you'll have to debug it yourself. The clue that Fredrik gave you is
            *not* of the use-once-and-discard variety -- when you are having
            problems with the pixies changing your lists into strings, you need to
            sprinkle prints of type(pixie_prey ) and repr(pixie_prey ) at salient
            points in your code; as first statement in that __init__ method would
            be a good start.

            Comment

            • Ben

              #7
              Re: Bizzare lst length problem

              Thanks for the advice - I'm already doing just that, so hopefully will
              soon be sorted :-p


              John Machin wrote:
              Ben wrote:
              Ah... my list is a string. That explains the len() results, but not why
              it is a string in the dirst place.

              I have a dictionary containing a number of instances of the following
              class as values:

              class panel:
              mops =[]

              def __init__(self,n umber,level,loc ation,mops,matr ix):
              self.number=num ber
              self.level=leve l
              self.location=l ocation
              self.mops=mops
              self.matrix=mat rix


              abve mops is a list, yet when I access it it is a string...
              >
              Well, if you are going to spare us from reading all of your code,
              you'll have to debug it yourself. The clue that Fredrik gave you is
              *not* of the use-once-and-discard variety -- when you are having
              problems with the pixies changing your lists into strings, you need to
              sprinkle prints of type(pixie_prey ) and repr(pixie_prey ) at salient
              points in your code; as first statement in that __init__ method would
              be a good start.

              Comment

              • John Machin

                #8
                Re: Bizzare lst length problem


                Ben wrote:
                ...and when I print out the string, it is still formatted as one would
                expect a list to be:
                >
                <type 'str'"['01', '02', '03', '04']"
                >
                We know that. Fredrik deduced it and told you well over an hour ago.

                Show us the code that is creating instances of the panel class ...

                panel1 =
                panel(number=?, level=?,locatio n=?,mops=?????? ??????????,matr ix=?)
                What are you passing as the 4th positional arg
                ^^^^^^^^^^^^^^^ ^^^^^^^^ ???

                Comment

                • Ben

                  #9
                  Re: Bizzare lst length problem

                  Using Fredericks advice I managed to track down the problem - it was
                  really very stupid. I had accidentally cast the list to a string
                  earlier in another part of the code. Its a bit of an anticlimax really
                  - not mysterious at all (just mysteriously remiss on my part)

                  Apologies for not simple posting the entire code earlier on - but
                  thanks for everyone for puttin up with me, and in particular to
                  Frederick for his very useful hint :-)

                  Cheers,

                  Ben


                  John Machin wrote:
                  Ben wrote:
                  ...and when I print out the string, it is still formatted as one would
                  expect a list to be:

                  <type 'str'"['01', '02', '03', '04']"
                  >
                  We know that. Fredrik deduced it and told you well over an hour ago.
                  >
                  Show us the code that is creating instances of the panel class ...
                  >
                  panel1 =
                  panel(number=?, level=?,locatio n=?,mops=?????? ??????????,matr ix=?)
                  What are you passing as the 4th positional arg
                  ^^^^^^^^^^^^^^^ ^^^^^^^^ ???

                  Comment

                  • Theerasak Photha

                    #10
                    Re: Bizzare lst length problem

                    On 8 Oct 2006 06:12:48 -0700, John Machin <sjmachin@lexic on.netwrote:
                    >
                    Show us the code that is creating instances of the panel class ...
                    >
                    panel1 =
                    panel(number=?, level=?,locatio n=?,mops=?????? ??????????,matr ix=?)
                    What are you passing as the 4th positional arg
                    ^^^^^^^^^^^^^^^ ^^^^^^^^ ???
                    This is wholly unnecessary.

                    -- Theerasak

                    Comment

                    • John Machin

                      #11
                      Re: Bizzare lst length problem


                      Theerasak Photha wrote:
                      On 8 Oct 2006 06:12:48 -0700, John Machin <sjmachin@lexic on.netwrote:
                      >

                      Show us the code that is creating instances of the panel class ...

                      panel1 =
                      panel(number=?, level=?,locatio n=?,mops=?????? ??????????,matr ix=?)
                      What are you passing as the 4th positional arg
                      ^^^^^^^^^^^^^^^ ^^^^^^^^ ???
                      >
                      This is wholly unnecessary.
                      >
                      -- Theerasak

                      What is wholly unnecessary?

                      Comment

                      • Ben

                        #12
                        Re: Bizzare lst length problem

                        Ah - I found out why I had cast it to a string. I had not, at that
                        point, worked out ho to pass the list by value rather than reference,
                        and so was casting to a string as a stopgap measure that I then forgot
                        about. Now the problem is fixed after this group told me how to pass a
                        list by value (by slicing the entire list)


                        John Machin wrote:
                        Theerasak Photha wrote:
                        On 8 Oct 2006 06:12:48 -0700, John Machin <sjmachin@lexic on.netwrote:
                        >
                        Show us the code that is creating instances of the panel class ...
                        >
                        panel1 =
                        panel(number=?, level=?,locatio n=?,mops=?????? ??????????,matr ix=?)
                        What are you passing as the 4th positional arg
                        ^^^^^^^^^^^^^^^ ^^^^^^^^ ???
                        This is wholly unnecessary.

                        -- Theerasak
                        >
                        >
                        What is wholly unnecessary?

                        Comment

                        • Fredrik Lundh

                          #13
                          Re: Bizzare lst length problem

                          Ben wrote:
                          Ah - I found out why I had cast it to a string. I had not, at that
                          point, worked out ho to pass the list by value rather than reference,
                          and so was casting to a string as a stopgap measure that I then forgot
                          about. Now the problem is fixed after this group told me how to pass a
                          list by value (by slicing the entire list)
                          if you write code that needs to treat a list as a distinct mutable
                          value, make sure *your* code makes a copy. relying on the caller to
                          remember to do that in all cases is way too error prone.

                          in other words, instead of doing

                          def function(seq):
                          # modify the sequence

                          ...

                          # must pass in a copy, or things will break in mysterious ways
                          function(list(m ylist))

                          do

                          def function(seq):
                          seq = list(seq) # make a distinct copy
                          # modify the sequence

                          ...

                          function(seq)

                          </F>

                          Comment

                          • John Machin

                            #14
                            Re: Bizzare lst length problem


                            Ben wrote:
                            Ah - I found out why I had cast it to a string. I had not, at that
                            point, worked out ho to pass the list by value rather than reference,
                            and so was casting to a string as a stopgap measure that I then forgot
                            about. Now the problem is fixed after this group told me how to pass a
                            list by value (by slicing the entire list)
                            >
                            All argument passing is by reference. What you are calling "pass a list
                            by value" is actually two steps:

                            (1) make a copy of a list
                            (2) pass a reference to the copy

                            At the C implementation level, it's a (PyObject *) -- the address of
                            the object.

                            HTH,
                            John

                            Comment

                            • Bruno Desthuilliers

                              #15
                              Re: Bizzare lst length problem

                              Ben wrote:
                              Ah... my list is a string. That explains the len() results, but not why
                              it is a string in the dirst place.
                              >
                              I have a dictionary containing a number of instances of the following
                              class as values:
                              >
                              class panel:
                              mops =[]
                              This one is a class attribute - it's shared between all instances of panel.
                              def __init__(self,n umber,level,loc ation,mops,matr ix):
                              self.number=num ber
                              self.level=leve l
                              self.location=l ocation
                              self.mops=mops
                              And here, you create an instance attribute with the same name, that will
                              shadow the class attribute.
                              self.matrix=mat rix
                              >
                              >
                              abve mops is a list, yet when I access it it is a string...
                              the class attribute 'mops' is a list. The instance attribute 'mops' is
                              whatever you passed when instanciating panel.


                              --
                              bruno desthuilliers
                              python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
                              p in 'onurb@xiludom. gro'.split('@')])"

                              Comment

                              Working...