Structures

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Paulo J. Matos

    Structures

    Hi all,

    I am a Python beginner, reading through 2.6 tutorial. I am wondering
    where are structures?

    On the other hand, I think I might have the answer. Since Python focus
    on having one way to do it and structures are something like classes
    with only public methods, if I want structures that's what I should use.
    Is that right?

    Cheers,
    --
    Paulo Jorge Matos - pocmatos at gmail.com
    Webpage: http://www.personal.soton.ac.uk/pocm
  • bearophileHUGS@lycos.com

    #2
    Re: Structures

    Paulo J. Matos:
    Since Python focus
    on having one way to do it and structures are something like classes
    with only public methods, if I want structures that's what I should use.
    Is that right?
    Yes, it is. On the other hand in Python 2.6 there's something that
    helps you build one of such classes in a standard and handy way: see
    "namedtuple " in the standard "collection s" module.

    Bye,
    bearophile

    Comment

    • Ben Finney

      #3
      Re: Structures

      "Paulo J. Matos" <pocmatos@gmail .comwrites:
      I am a Python beginner, reading through 2.6 tutorial. I am wondering
      where are structures?
      I'm wondering a more fundamental question: What are structures? That
      is, what do *you* mean by that term; without knowing that, an answer
      isn't likely to be meaningful.
      […] structures are something like classes with only public methods
      Care to say more about what they are, not what they're like?

      --
      \ “Pinky, are you pondering what I'm pondering?” “Yes Brain, but |
      `\ if our knees bent the other way, how would we ride a bicycle?” |
      _o__) —_Pinky and The Brain_ |
      Ben Finney

      Comment

      • Paulo J. Matos

        #4
        Re: Structures

        On Mon, Nov 3, 2008 at 12:32 PM, Ben Finney
        <bignose+hate s-spam@benfinney. id.auwrote:
        "Paulo J. Matos" <pocmatos@gmail .comwrites:
        >
        >I am a Python beginner, reading through 2.6 tutorial. I am wondering
        >where are structures?
        >
        I'm wondering a more fundamental question: What are structures? That
        is, what do *you* mean by that term; without knowing that, an answer
        isn't likely to be meaningful.
        >
        >[…] structures are something like classes with only public methods
        >
        Care to say more about what they are, not what they're like?
        >
        Well, I guess that everyone pretty much gets since it exists in every
        other language as struct, or define-structure, or whatever is the
        syntax. Still, answering your rhetoric question, a structure is way to
        gather information by fields and those fields are referenced by name.
        The fact that python 2.6 has now named tuples is a breath of fresh
        air!
        --
        \ "Pinky, are you pondering what I'm pondering?" "Yes Brain, but |
        `\ if our knees bent the other way, how would we ride a bicycle?" |
        _o__) —_Pinky and The Brain_ |
        Ben Finney
        --

        >
        >
        >


        --
        Paulo Jorge Matos - pocmatos at gmail.com
        Webpage: http://www.personal.soton.ac.uk/pocm

        Comment

        • Ben Finney

          #5
          Re: Structures

          "Paulo J. Matos" <pocmatos@gmail .comwrites:
          On Mon, Nov 3, 2008 at 12:32 PM, Ben Finney
          <bignose+hate s-spam@benfinney. id.auwrote:
          I'm wondering a more fundamental question: What are structures?
          That is, what do *you* mean by that term; without knowing that, an
          answer isn't likely to be meaningful.
          >
          Well, I guess that everyone pretty much gets since it exists in
          every other language as struct, or define-structure, or whatever is
          the syntax.
          Take care with broad sweeping statements about “every other language”,
          or even “most other languages”. They are usually flat-out wrong:
          there is a stunning variety of different approaches and concepts in
          programming languages, with very little common to even a majority of
          them.

          So no, the question was entirely honest and not a rhetorical device.
          Still, answering your rhetoric question, a structure is way to
          gather information by fields and those fields are referenced by
          name.
          Okay, you're talking about ‘struct’ from the C language. That helps
          answer the question.

          In Python, the way to do that is with a dict. A class can be used, but
          is often overkill if one doesn't need customised behaviour.
          The fact that python 2.6 has now named tuples is a breath of fresh
          air!
          That works also, but a dict will be more broadly useful; and
          compatible with any Python version.

          --
          \ “Creativity can be a social contribution, but only in so far as |
          `\ society is free to use the results.” —Richard Stallman |
          _o__) |
          Ben Finney

          Comment

          • Jorgen Grahn

            #6
            Re: Structures

            On Mon, 3 Nov 2008 20:33:45 +0000, Paulo J. Matos <pocmatos@gmail .comwrote:
            On Mon, Nov 3, 2008 at 12:32 PM, Ben Finney
            <bignose+hate s-spam@benfinney. id.auwrote:
            >"Paulo J. Matos" <pocmatos@gmail .comwrites:
            ....
            >I'm wondering a more fundamental question: What are structures? That
            >is, what do *you* mean by that term; without knowing that, an answer
            >isn't likely to be meaningful.
            >>
            >>[?] structures are something like classes with only public methods
            >>
            >Care to say more about what they are, not what they're like?
            >
            Well, I guess that everyone pretty much gets since it exists in every
            other language as struct, or define-structure, or whatever is the
            syntax.
            You're right. But the explanation you gave above, "classes with only
            public methods", is C++-specific, and also fairly misleading ... there
            is no real difference in C++ between struct Foo {}; and a class Foo
            {}; it's just shorthand for class Foo { public: }; and struct Foo {
            private: }; respectively.

            /Jorgen

            --
            // Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
            \X/ snipabacken.se R'lyeh wgah'nagl fhtagn!

            Comment

            • Arnaud Delobelle

              #7
              Re: Structures

              Ben Finney <bignose+hate s-spam@benfinney. id.auwrites:
              "Paulo J. Matos" <pocmatos@gmail .comwrites:
              [...]
              Okay, you're talking about ‘struct’ from the C language. That helps
              answer the question.
              Note that structs are mutable.
              In Python, the way to do that is with a dict. A class can be used, but
              is often overkill if one doesn't need customised behaviour.
              >
              >The fact that python 2.6 has now named tuples is a breath of fresh
              >air!
              But isn't mutable, so it doesn't seem to be what you (Paulo) need.
              That works also, but a dict will be more broadly useful; and
              compatible with any Python version.
              And is mutable.

              --
              Arnaud

              Comment

              • Aaron Brady

                #8
                Re: Structures

                On Nov 3, 3:45 pm, Ben Finney <bignose+hate s-s...@benfinney. id.au>
                wrote:
                "Paulo J. Matos" <pocma...@gmail .comwrites:
                >
                On Mon, Nov 3, 2008 at 12:32 PM, Ben Finney
                <bignose+hate s-s...@benfinney. id.auwrote:
                I'm wondering a more fundamental question: What are structures?
                That is, what do *you* mean by that term; without knowing that, an
                answer isn't likely to be meaningful.
                >
                Well, I guess that everyone pretty much gets since it exists in
                every other language as struct, or define-structure, or whatever is
                the syntax.
                >
                Take care with broad sweeping statements about “every other language”,
                or even “most other languages”. They are usually flat-out wrong:
                there is a stunning variety of different approaches and concepts in
                programming languages, with very little common to even a majority of
                them.
                Yea, verily. How many languages do you think that is? Feel free to
                count C and C++ as different ones.

                "Four shalt thou not count, neither count thou two...."


                Comment

                • Craig Allen

                  #9
                  Re: Structures

                  >
                  Care to say more about what they are, not what they're like?
                  >
                  I'm not the OP and I may be biased by C++, I can imagine the
                  complaints when I say, classes are just structures with function
                  members for working on the structure.

                  Comment

                  • Arnaud Delobelle

                    #10
                    Re: Structures

                    Craig Allen <callen314@gmai l.comwrites:
                    >>
                    >Care to say more about what they are, not what they're like?
                    >>
                    >
                    I'm not the OP and I may be biased by C++, I can imagine the
                    complaints when I say, classes are just structures with function
                    members for working on the structure.
                    In C++ classes and structures are the same, except that class members
                    are private by default and structure members are public by default

                    --
                    Arnaud

                    Comment

                    • Paulo J. Matos

                      #11
                      Re: Structures

                      On Mon, Nov 3, 2008 at 10:10 PM, Arnaud Delobelle
                      <arnodel@google mail.comwrote:
                      Ben Finney <bignose+hate s-spam@benfinney. id.auwrites:
                      >
                      >"Paulo J. Matos" <pocmatos@gmail .comwrites:
                      [...]
                      >Okay, you're talking about 'struct' from the C language. That helps
                      >answer the question.
                      >
                      Note that structs are mutable.
                      >
                      Ah... :( That's a no go!
                      >In Python, the way to do that is with a dict. A class can be used, but
                      >is often overkill if one doesn't need customised behaviour.
                      >>
                      >>The fact that python 2.6 has now named tuples is a breath of fresh
                      >>air!
                      >
                      But isn't mutable, so it doesn't seem to be what you (Paulo) need.
                      >
                      What's then the reason for adding named tuples if they are not mutable...???
                      >That works also, but a dict will be more broadly useful; and
                      >compatible with any Python version.
                      >
                      And is mutable.
                      >
                      Even though I can use dicts where the keys are strings (as if it were
                      the name of the field), it seems to heavy, since a structure doesn't
                      need to be resizable (and dicts are) and it has constant time access
                      (which depending on the implementation I would guess dicts don't
                      have).

                      Can someone please clarify?

                      Cheers,

                      Paulo


                      --
                      Paulo Jorge Matos - pocmatos at gmail.com
                      Webpage: http://www.personal.soton.ac.uk/pocm

                      Comment

                      • Paulo J. Matos

                        #12
                        Re: Structures

                        On Mon, Nov 3, 2008 at 10:19 PM, Aaron Brady <castironpi@gma il.comwrote:
                        On Nov 3, 3:45 pm, Ben Finney <bignose+hate s-s...@benfinney. id.au>
                        wrote:
                        >"Paulo J. Matos" <pocma...@gmail .comwrites:
                        >>
                        On Mon, Nov 3, 2008 at 12:32 PM, Ben Finney
                        <bignose+hate s-s...@benfinney. id.auwrote:
                        I'm wondering a more fundamental question: What are structures?
                        That is, what do *you* mean by that term; without knowing that, an
                        answer isn't likely to be meaningful.
                        >>
                        Well, I guess that everyone pretty much gets since it exists in
                        every other language as struct, or define-structure, or whatever is
                        the syntax.
                        >>
                        >Take care with broad sweeping statements about "every other language",
                        >or even "most other languages". They are usually flat-out wrong:
                        >there is a stunning variety of different approaches and concepts in
                        >programming languages, with very little common to even a majority of
                        >them.
                        >
                        Yea, verily. How many languages do you think that is? Feel free to
                        count C and C++ as different ones.
                        >
                        "Four shalt thou not count, neither count thou two...."

                        >
                        Well, I wouldn't dare to say I know a lot of languages but the ones I
                        do provide mechanisms to define structures / records: C, C++, Scheme,
                        Common Lisp, Haskell, SML, Ocaml.
                        This is obviously a minority if you count all available programming
                        languages in the world, but I would dare to say these cover a lot of
                        ground.

                        However, I wouldn't dare to say Python needs structures to be a good
                        language, or anything similar. My question was more directed to : if
                        there aren't structures in Python, what do Pythonists use instead?
                        (I have seen dicts might be an alternative, but as I said in previous
                        post, they seem to flexible [making them a canon to shoot a fly, and
                        they probably lack constant-time access, right?]


                        --
                        Paulo Jorge Matos - pocmatos at gmail.com
                        Webpage: http://www.personal.soton.ac.uk/pocm

                        Comment

                        • Joe Strout

                          #13
                          Re: Structures

                          On Nov 3, 2008, at 4:38 PM, Paulo J. Matos wrote:
                          However, I wouldn't dare to say Python needs structures to be a good
                          language, or anything similar. My question was more directed to : if
                          there aren't structures in Python, what do Pythonists use instead?
                          Classes.

                          Best,
                          - Joe


                          Comment

                          • Paulo J. Matos

                            #14
                            Re: Structures

                            On Mon, Nov 3, 2008 at 11:47 PM, Joe Strout <joe@strout.net wrote:
                            On Nov 3, 2008, at 4:38 PM, Paulo J. Matos wrote:
                            >
                            >However, I wouldn't dare to say Python needs structures to be a good
                            >language, or anything similar. My question was more directed to : if
                            >there aren't structures in Python, what do Pythonists use instead?
                            >
                            Classes.
                            >
                            ok, so that goes back to what I said on my first post. Classes with no
                            private fields/methods.

                            Thanks,

                            Paulo


                            --
                            Paulo Jorge Matos - pocmatos at gmail.com
                            Webpage: http://www.personal.soton.ac.uk/pocm

                            Comment

                            • George Sakkis

                              #15
                              Re: Structures

                              On Nov 3, 6:51 pm, "Paulo J. Matos" <pocma...@gmail .comwrote:
                              On Mon, Nov 3, 2008 at 11:47 PM, Joe Strout <j...@strout.ne twrote:
                              On Nov 3, 2008, at 4:38 PM, Paulo J. Matos wrote:
                              >
                              However, I wouldn't dare to say Python needs structures to be a good
                              language, or anything similar. My question was more directed to : if
                              there aren't structures in Python, what do Pythonists use instead?
                              >
                              Classes.
                              >
                              ok, so that goes back to what I said on my first post. Classes with no
                              private fields/methods.
                              Technically there are no private attributes in (pure) Python so the
                              answer is still classes.

                              George

                              Comment

                              Working...