Collections of non-arbitrary objects ?

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

    #1

    Collections of non-arbitrary objects ?

    Python seems to have a log of ways to do collections of arbitrary
    objects: lists, tuples, dictionaries. But what if I want a collection
    of non-arbitrary objects? A list of records, or something like that?

  • Larry Bates

    #2
    Re: Collections of non-arbitrary objects ?

    walterbyrd wrote:
    Python seems to have a log of ways to do collections of arbitrary
    objects: lists, tuples, dictionaries. But what if I want a collection
    of non-arbitrary objects? A list of records, or something like that?
    >
    Code the record as an object (class) and place instances of those objects inside
    a list or dictionary (depending on access required). Or if you don't want to do
    much with the records just put each instance of a record (normally a record is
    implemented as a tuple at least thats how SQL databases return rows) inside a
    list or a dictionary (again depending on access required).

    -Larry

    Comment

    • bruno.desthuilliers@gmail.com

      #3
      Re: Collections of non-arbitrary objects ?

      On Jun 21, 8:19 pm, walterbyrd <walterb...@ina me.comwrote:
      Python seems to have a log of ways to do collections of arbitrary
      objects: lists, tuples, dictionaries.
      And sets.
      But what if I want a collection
      of non-arbitrary objects?
      Then only put the kind of objects you want in the collection.
      A list of records, or something like that
      'Records' can be implemented as tuples, dicts or using a custom class.

      Comment

      • Ben Finney

        #4
        Re: Collections of non-arbitrary objects ?

        walterbyrd <walterbyrd@ina me.comwrites:
        Python seems to have a log of ways to do collections of arbitrary
        objects: lists, tuples, dictionaries. But what if I want a
        collection of non-arbitrary objects? A list of records, or something
        like that?
        Then collect them in a non-arbitrary way.

        That's a flippant response, but I don't understand the question. What
        are you asking for that you don't already have? A list can contain a
        sequence of objects of the same type already. What are you expecting
        that a list does not provide?

        --
        \ "Faith may be defined briefly as an illogical belief in the |
        `\ occurrence of the improbable." -- Henry L. Mencken |
        _o__) |
        Ben Finney

        Comment

        • Grant Edwards

          #5
          Re: Collections of non-arbitrary objects ?

          On 2007-06-21, Ben Finney <bignose+hate s-spam@benfinney. id.auwrote:
          walterbyrd <walterbyrd@ina me.comwrites:
          >
          >Python seems to have a log of ways to do collections of
          >arbitrary objects: lists, tuples, dictionaries. But what if I
          >want a collection of non-arbitrary objects?
          Lists never contain arbitrary objects, they contain only
          objects that you put there. The reason that other languages
          have typed containers (e.g. array of type T) is that the
          elements of the array don't know what type they are, so you've
          got to limit what you put in there. In python, all objects
          know what type they are, so there's no point in labelling the
          references to the objects with type info as well.
          >A list of records, or something like that?
          >
          Then collect them in a non-arbitrary way.
          >
          That's a flippant response, but I don't understand the
          question. What are you asking for that you don't already have?
          A list can contain a sequence of objects of the same type
          already. What are you expecting that a list does not provide?
          I was also a bit baffled by the question. The only things I
          could think of are:

          1) a "container" that raised an exception if the type of a new
          item doesn't match the type of what's in it already. I
          don't really see much benefit in that. If you want a list
          to contain only objects of type T, then only put that type
          of objects in it.

          2) an "array" that contains a large number of small things
          (e.g. integer or floating point numbers) that need to be
          stored with minimal overhead. That's a useful thing, and
          there are several packages that do that -- numpy is the one
          generally recommended for new designs.

          --
          Grant Edwards grante Yow! Are the STEWED PRUNES
          at still in the HAIR DRYER?
          visi.com

          Comment

          • walterbyrd

            #6
            Re: Collections of non-arbitrary objects ?

            On Jun 21, 5:38 pm, Ben Finney <bignose+hate s-s...@benfinney. id.au>
            wrote:
            That's a flippant response, but I don't understand the question.
            Everybody here seems to have about the same response: "why would you
            ever want to do that?"

            Maybe it's something that doesn't "need" to be done, but it seems to
            me that would give you a certain level of built-in integrity - you
            could be sure about what's in the structure. I would not expect that
            all of python would be that rigid, but I thought it might be
            worthwhile if there were one such structure.

            Think of this: why use tuples when lists are available? You can use a
            tuple to index a dictionary, but not a list - why? I the answer may
            be that sometimes you want some degree on inherent enforced structure.
            I'm sure the language could have designed to allow lists to index
            dictionary, but you would have to awfully careful with those lists.
            The burden would be on the programmer to make certain that those lists
            didn't change. But, it could be done, therefore tuples are not
            "needed" right?

            Languages like C are often criticized as being too rigid - you have to
            pre-define your variables, and pre-allocate your array sizes. But,
            languages like Perl and BASIC, are often criticized as being to
            sloppy - too few restrictions tend to lead to sloppy code. So I guess
            there is a sort of trade-off.

            I suppose I could use a database, that might give me some degree of
            assured integrity - depending on what database I used.

            Comment

            • Ben Finney

              #7
              Re: Collections of non-arbitrary objects ?

              walterbyrd <walterbyrd@ina me.comwrites:
              Maybe it's something that doesn't "need" to be done, but it seems to
              me that would give you a certain level of built-in integrity - you
              could be sure about what's in the structure. I would not expect that
              all of python would be that rigid, but I thought it might be
              worthwhile if there were one such structure.
              Can you help us understand, by showing a use case that would in your
              estimation be improved by the feature you're describing?

              --
              \ "Quidquid latine dictum sit, altum viditur." ("Whatever is |
              `\ said in Latin, sounds profound.") -- Anonymous |
              _o__) |
              Ben Finney

              Comment

              • Gabriel Genellina

                #8
                Re: Collections of non-arbitrary objects ?

                En Fri, 22 Jun 2007 21:45:02 -0300, walterbyrd <walterbyrd@ina me.com>
                escribió:
                Maybe it's something that doesn't "need" to be done, but it seems to
                me that would give you a certain level of built-in integrity - you
                could be sure about what's in the structure. I would not expect that
                all of python would be that rigid, but I thought it might be
                worthwhile if there were one such structure.
                Sure. You can implement it yourself, if you want; it's not so hard even
                for a beginner. It's just not needed enough, or required enough, to become
                a builtin type.
                Look for some recent posts about a RestrictedList.

                --
                Gabriel Genellina

                Comment

                • Paddy

                  #9
                  Re: Collections of non-arbitrary objects ?

                  On Jun 23, 1:45 am, walterbyrd <walterb...@ina me.comwrote:
                  On Jun 21, 5:38 pm, Ben Finney <bignose+hate s-s...@benfinney. id.au>
                  wrote:
                  >
                  That's a flippant response, but I don't understand the question.
                  >
                  Everybody here seems to have about the same response: "why would you
                  ever want to do that?"
                  >
                  Maybe it's something that doesn't "need" to be done, but it seems to
                  me that would give you a certain level of built-in integrity - you
                  could be sure about what's in the structure. I would not expect that
                  all of python would be that rigid, but I thought it might be
                  worthwhile if there were one such structure.
                  >
                  Think of this: why use tuples when lists are available? You can use a
                  tuple to index a dictionary, but not a list - why? I the answer may
                  be that sometimes you want some degree on inherent enforced structure.
                  I'm sure the language could have designed to allow lists to index
                  dictionary, but you would have to awfully careful with those lists.
                  The burden would be on the programmer to make certain that those lists
                  didn't change. But, it could be done, therefore tuples are not
                  "needed" right?
                  >
                  Languages like C are often criticized as being too rigid - you have to
                  pre-define your variables, and pre-allocate your array sizes. But,
                  languages like Perl and BASIC, are often criticized as being to
                  sloppy - too few restrictions tend to lead to sloppy code. So I guess
                  there is a sort of trade-off.
                  >
                  I suppose I could use a database, that might give me some degree of
                  assured integrity - depending on what database I used.
                  Hi Walterbyrd,
                  What happens when you are given good advice that may be contrary to
                  intuition?

                  Unfortunately its how we usually do things in Python and do NOT
                  suffer because of it. Try writing your application without it. Test
                  without it. Write other applications without it. Others do,
                  successfully.

                  - Paddy.


                  Comment

                  • walterbyrd

                    #10
                    Re: Collections of non-arbitrary objects ?

                    On Jun 22, 11:43 pm, Ben Finney <bignose+hate s-s...@benfinney. id.au>
                    wrote:
                    Can you help us understand, by showing a use case that would in your
                    estimation be improved by the feature you're describing?
                    >
                    Suppose you are sequentially processing a list with a routine that
                    expects every item to be of a certain type. Something in the list that
                    doesn't conform to the type could give you unexpected results, maybe
                    crash your application.

                    In python, as far as I know, there is nothing built into the language
                    to keep any type of item from being included in a list - or any such
                    structure. To me, that seems like a potentially vulnerability.
                    Especially since variables in python do not have to be explicitly
                    assigned - another variable that points to the same thing, could
                    change the data that a variable points to.



                    Comment

                    • 7stud

                      #11
                      Re: Collections of non-arbitrary objects ?

                      On Jun 23, 11:45 am, walterbyrd <walterb...@ina me.comwrote:
                      On Jun 22, 11:43 pm, Ben Finney <bignose+hate s-s...@benfinney. id.au>
                      wrote:
                      >
                      Can you help us understand, by showing a use case that would in your
                      estimation be improved by the feature you're describing?
                      >
                      Suppose you are sequentially processing a list with a routine that
                      expects every item to be of a certain type. Something in the list that
                      doesn't conform to the type could give you unexpected results, maybe
                      crash your application.
                      >
                      if hasattr(elmt, some_func):
                      elmt.some_func( )



                      Comment

                      • Marc 'BlackJack' Rintsch

                        #12
                        Re: Collections of non-arbitrary objects ?

                        In <1182620734.940 718.196520@e16g 2000pri.googleg roups.com>, walterbyrd
                        wrote:
                        On Jun 22, 11:43 pm, Ben Finney <bignose+hate s-s...@benfinney. id.au>
                        wrote:
                        >
                        >Can you help us understand, by showing a use case that would in your
                        >estimation be improved by the feature you're describing?
                        >>
                        >
                        Suppose you are sequentially processing a list with a routine that
                        expects every item to be of a certain type. Something in the list that
                        doesn't conform to the type could give you unexpected results, maybe
                        crash your application.
                        It raises an exception. What want you an "typed list" to do when a wrong
                        object is put into it? Raising an exception? So all you change is the
                        point in time when the exception is raised.
                        In python, as far as I know, there is nothing built into the language
                        to keep any type of item from being included in a list - or any such
                        structure. To me, that seems like a potentially vulnerability.
                        Especially since variables in python do not have to be explicitly
                        assigned - another variable that points to the same thing, could
                        change the data that a variable points to.
                        But this doesn't really change with a "typed list". It's easy to take
                        an object and completely replace all its attributes so it behaves very
                        different.

                        Ciao,
                        Marc 'BlackJack' Rintsch

                        Comment

                        • Paddy

                          #13
                          Re: Collections of non-arbitrary objects ?

                          On Jun 23, 6:45 pm, walterbyrd <walterb...@ina me.comwrote:
                          On Jun 22, 11:43 pm, Ben Finney <bignose+hate s-s...@benfinney. id.au>
                          wrote:
                          >
                          Can you help us understand, by showing a use case that would in your
                          estimation be improved by the feature you're describing?
                          >
                          Suppose you are sequentially processing a list with a routine that
                          expects every item to be of a certain type. Something in the list that
                          doesn't conform to the type could give you unexpected results, maybe
                          crash your application.
                          >
                          In python, as far as I know, there is nothing built into the language
                          to keep any type of item from being included in a list - or any such
                          structure. To me, that seems like a potentially vulnerability.
                          Especially since variables in python do not have to be explicitly
                          assigned - another variable that points to the same thing, could
                          change the data that a variable points to.
                          Reminds me a bit of that (awful) sketch:
                          Patient: Doctor doctor it hurts if I do that.
                          Doctor: Well don't do that then.

                          The data for the list should have been checked when it entered
                          your program. It is up to you to then only stuff the list with
                          data expected by the routine. If you don't then Python will most
                          likely throw a runtime exception, but it is up to you to trust
                          your co-workers on the project.

                          - Paddy

                          Comment

                          • Bjoern Schliessmann

                            #14
                            Re: Collections of non-arbitrary objects ?

                            7stud wrote:
                            On Jun 23, 11:45 am, walterbyrd <walterb...@ina me.comwrote:
                            >Suppose you are sequentially processing a list with a routine
                            >that expects every item to be of a certain type. Something in the
                            >list that doesn't conform to the type could give you unexpected
                            >results, maybe crash your application.
                            >>
                            >
                            if hasattr(elmt, some_func):
                            elmt.some_func( )
                            Personally, I prefer

                            try:
                            elmt.some_func( )
                            except AttributeError:
                            # do stuff

                            Regards,


                            Björn

                            --
                            BOFH excuse #130:

                            new management

                            Comment

                            • Ben Finney

                              #15
                              Re: Collections of non-arbitrary objects ?

                              walterbyrd <walterbyrd@ina me.comwrites:
                              Suppose you are sequentially processing a list with a routine that
                              expects every item to be of a certain type. Something in the list
                              that doesn't conform to the type could give you unexpected results,
                              maybe crash your application.
                              A routine that demands its inputs to be of a certain *type*, rather
                              than requiring that the implement the required *behaviour*, breaks
                              polymorphism.

                              Polymorphism is considered valuable in Python; search for any of
                              "polymorphi sm", "duck typing" and "easier to ask forgiveness than
                              permission".

                              --
                              \ "Intellectu al property is to the 21st century what the slave |
                              `\ trade was to the 16th." -- David Mertz |
                              _o__) |
                              Ben Finney

                              Comment

                              Working...