dicts vs classes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Guyon Morée

    #1

    dicts vs classes

    Hi all,

    I'm using simple classes as a container of named values and I'm
    instantiating a lot of them in a very short time.

    i was wondering if there is any benefit in using dicts instead from a
    performance/memory usage point of view?

    regards,

    Guyon Morée


  • Marc 'BlackJack' Rintsch

    #2
    Re: dicts vs classes

    In <1153821590.452 329.208790@i3g2 000cwc.googlegr oups.com>, Guyon Morée
    wrote:
    I'm using simple classes as a container of named values and I'm
    instantiating a lot of them in a very short time.
    >
    i was wondering if there is any benefit in using dicts instead from a
    performance/memory usage point of view?
    If you really have a memory problem read the documentation about
    `__slots__`. But I would only consider this if `a lot of` is several 100k
    or millions of objects and the memory consumption really is a problem.

    Ciao,
    Marc 'BlackJack' Rintsch

    Comment

    • Marco Wahl

      #3
      Re: dicts vs classes

      "Guyon Morée" <guyon.moree@gm ail.comwrites:
      I'm using simple classes as a container of named values and I'm
      instantiating a lot of them in a very short time.
      >
      i was wondering if there is any benefit in using dicts instead from a
      performance/memory usage point of view?
      I recommend you to measure the time and memory usage
      for the two alternatives. That could give you the
      answer you want.


      HTH
      --
      Marco Wahl

      Comment

      • Pan Xingzhi

        #4
        Re: dicts vs classes

        dict is already a class....why another?

        Guyon Morée wrote:
        Hi all,
        >
        I'm using simple classes as a container of named values and I'm
        instantiating a lot of them in a very short time.
        >
        i was wondering if there is any benefit in using dicts instead from a
        performance/memory usage point of view?
        >
        regards,
        >
        Guyon Morée

        >

        Comment

        • Simon Hibbs

          #5
          Re: dicts vs classes

          I'm wondering about whether to use objects in this way or dictionaries
          for a program I'm writing at the moment. It seems to me that unless you
          need some of the functionality supplied with dictionaries (len(a),
          has_key, etc) then simple objects are a syntacticaly cleaner and more
          natural way to express yourself.

          Any objctions to this, or pitfalls?

          Simon Hibbs

          Comment

          • Aahz

            #6
            Don't use __slots__! (was Re: dicts vs classes)

            In article <pan.2006.07.25 .10.11.28.30357 6@gmx.net>,
            Marc 'BlackJack' Rintsch <bj_666@gmx.net wrote:
            >In <1153821590.452 329.208790@i3g2 000cwc.googlegr oups.com>, Guyon Morée
            >wrote:
            >>
            >I'm using simple classes as a container of named values and I'm
            >instantiatin g a lot of them in a very short time.
            >>
            >i was wondering if there is any benefit in using dicts instead from a
            >performance/memory usage point of view?
            >
            >If you really have a memory problem read the documentation about
            >`__slots__`. But I would only consider this if `a lot of` is several 100k
            >or millions of objects and the memory consumption really is a problem.
            Guido sez:

            __slots__ is a terrible hack with nasty, hard-to-fathom side
            effects that should only be used by programmers at grandmaster and
            wizard levels. Unfortunately it has gained an enormous undeserved
            popularity amongst the novices and apprentices, who should know
            better than to use this magic incantation casually.
            --
            Aahz (aahz@pythoncra ft.com) <* http://www.pythoncraft.com/

            "Debugging is twice as hard as writing the code in the first place.
            Therefore, if you write the code as cleverly as possible, you are, by
            definition, not smart enough to debug it." --Brian W. Kernighan

            Comment

            • bearophileHUGS@lycos.com

              #7
              Re: dicts vs classes

              Simon Hibbs:
              It seems to me that unless you
              need some of the functionality supplied with dictionaries (len(a),
              has_key, etc) then simple objects are a syntacticaly cleaner and more
              natural way to express yourself.
              I'd say the opposite. Classes contain a dict of their attributes, etc.
              So if you don't need the functionality supplied by objects, then using
              simpler dictionaries is better. (But in the end the choice has to be
              made according to the specific situations).

              Bye,
              bearophile

              Comment

              • bearophileHUGS@lycos.com

                #8
                Re: Don't use __slots__! (was Re: dicts vs classes)

                Aahz, citing Guido:
                >__slots__ is a terrible hack with nasty, hard-to-fathom side
                >effects that should only be used by programmers at grandmaster and
                >wizard levels. Unfortunately it has gained an enormous undeserved
                I think I have used __slots__ just one time. Can you tell me some of of
                such bad side effects?

                Bye,
                bearophile

                Comment

                • Simon Forman

                  #9
                  Re: dicts vs classes

                  Simon Hibbs wrote:
                  I'm wondering about whether to use objects in this way or dictionaries
                  for a program I'm writing at the moment. It seems to me that unless you
                  need some of the functionality supplied with dictionaries (len(a),
                  has_key, etc) then simple objects are a syntacticaly cleaner and more
                  natural way to express yourself.
                  >
                  Any objctions to this, or pitfalls?
                  >
                  Simon Hibbs
                  I'm not sure, but I think this should be the other way round: unless
                  you need special behavior that dicts don't supply (methods) or you
                  really want/need obj.attr notation, you're better off just using dicts,
                  but Marco Wahl is right, if it really matters measure it.

                  Peace,
                  ~Simon

                  Comment

                  • Aahz

                    #10
                    Re: Don't use __slots__! (was Re: dicts vs classes)

                    In article <1153843248.761 424.246730@i3g2 000cwc.googlegr oups.com>,
                    <bearophileHUGS @lycos.comwrote :
                    >Aahz, citing Guido:
                    >>
                    >>__slots__ is a terrible hack with nasty, hard-to-fathom side
                    >>effects that should only be used by programmers at grandmaster and
                    >>wizard levels. Unfortunately it has gained an enormous undeserved
                    >
                    >I think I have used __slots__ just one time. Can you tell me some of of
                    >such bad side effects?
                    The main one is that inheritance becomes difficult to nearly-impossible.
                    --
                    Aahz (aahz@pythoncra ft.com) <* http://www.pythoncraft.com/

                    "Debugging is twice as hard as writing the code in the first place.
                    Therefore, if you write the code as cleverly as possible, you are, by
                    definition, not smart enough to debug it." --Brian W. Kernighan

                    Comment

                    • Roy Smith

                      #11
                      Re: Don't use __slots__! (was Re: dicts vs classes)

                      >Guido sez:
                      >
                      __slots__ is a terrible hack with nasty, hard-to-fathom side
                      effects that should only be used by programmers at grandmaster and
                      wizard levels. Unfortunately it has gained an enormous undeserved
                      popularity amongst the novices and apprentices, who should know
                      better than to use this magic incantation casually.
                      But, if they are novices, why should they be expected to know better?

                      I just re-read http://docs.python.org/ref/slots.html#l2h-217 and don't
                      see anyplace where it says, "Warning: for use by wizards only".

                      Comment

                      • Nick Vatamaniuc

                        #12
                        Re: dicts vs classes

                        Don't optimize prematurely. Write whatever is cleaner, simpler and
                        makes more sense. Such that if someone (or even yourself) looks at it
                        10 years from now they'll know exactly what is going on. As far as
                        what is slower or what functionality you will use and what you won't --
                        well, if you won't use it then why worry about it and to find out if it
                        is slower run a quick benchmark and you'll know...
                        -NickV

                        bearophileHUGS@ lycos.com wrote:
                        Simon Hibbs:
                        It seems to me that unless you
                        need some of the functionality supplied with dictionaries (len(a),
                        has_key, etc) then simple objects are a syntacticaly cleaner and more
                        natural way to express yourself.
                        >
                        I'd say the opposite. Classes contain a dict of their attributes, etc.
                        So if you don't need the functionality supplied by objects, then using
                        simpler dictionaries is better. (But in the end the choice has to be
                        made according to the specific situations).
                        >
                        Bye,
                        bearophile

                        Comment

                        • Aahz

                          #13
                          Re: Don't use __slots__! (was Re: dicts vs classes)

                          In article <ea5jd5$ia8$1@r eader2.panix.co m>, Roy Smith <roy@panix.comw rote:
                          >>Guido sez:
                          >>
                          > __slots__ is a terrible hack with nasty, hard-to-fathom side
                          > effects that should only be used by programmers at grandmaster and
                          > wizard levels. Unfortunately it has gained an enormous undeserved
                          > popularity amongst the novices and apprentices, who should know
                          > better than to use this magic incantation casually.
                          >
                          >But, if they are novices, why should they be expected to know better?
                          >
                          >I just re-read http://docs.python.org/ref/slots.html#l2h-217 and don't
                          >see anyplace where it says, "Warning: for use by wizards only".
                          Unfortunately, the Python docs are currently suboptimal when it comes to
                          new-style classes.
                          --
                          Aahz (aahz@pythoncra ft.com) <* http://www.pythoncraft.com/

                          "Debugging is twice as hard as writing the code in the first place.
                          Therefore, if you write the code as cleverly as possible, you are, by
                          definition, not smart enough to debug it." --Brian W. Kernighan

                          Comment

                          Working...