Why a class when there will only be one instance?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • SeeBelow@SeeBelow.Nut

    Why a class when there will only be one instance?

    I see the value of a class when two or more instances will be created,
    but Python programmers regularly use a class when there will only be one
    instance.
    What is the benefit of this? It has a disadvantage of a whole lot of
    "self."
    being required everywhere, making the code less readable. Also, since a
    strength of Python is rapid application development, it slows one down
    to have to put in all those self.'s. The code seems much cleaner to me
    without classes that have only one instance. Oh, also, all the methods
    of this class will have to have the instance name prepended to them.

    I would appreciate it if someone could explain the advantages of doing
    this, or at least the sociological reasons why it occurs.

    Mitchell Timin

    --
    "Many are stubborn in pursuit of the path they have chosen, few in
    pursuit of the goal." - Friedrich Nietzsche

    http://annevolve.sourceforge.net is what I'm into nowadays.
    Humans may write to me at this address: zenguy at shaw dot ca
  • Jorge Godoy

    #2
    Re: Why a class when there will only be one instance?

    On Ter 25 Mai 2004 21:43, SeeBelow@SeeBel ow.Nut wrote:
    [color=blue]
    > I see the value of a class when two or more instances will be created,
    > but Python programmers regularly use a class when there will only be one
    > instance.
    > What is the benefit of this? It has a disadvantage of a whole lot of
    > "self."
    > being required everywhere, making the code less readable. Also, since a
    > strength of Python is rapid application development, it slows one down
    > to have to put in all those self.'s. The code seems much cleaner to me
    > without classes that have only one instance. Oh, also, all the methods
    > of this class will have to have the instance name prepended to them.
    >
    > I would appreciate it if someone could explain the advantages of doing
    > this, or at least the sociological reasons why it occurs.[/color]

    How can you guarantee that such a code will never ever be used again,
    anywhere?

    It's easier to allow for code reuse from the beginning, IMHO.

    --
    Godoy. <godoy@ieee.org >

    Comment

    • Steve Lamb

      #3
      Re: Why a class when there will only be one instance?

      On 2004-05-26, SeeBelow@SeeBel ow.Nut <SeeBelow@SeeBe low.Nut> wrote:[color=blue]
      > I see the value of a class when two or more instances will be created, but
      > Python programmers regularly use a class when there will only be one
      > instance. What is the benefit of this?[/color]

      What happens when down the line someone wants to use that code in
      something that requires multiple instances?

      --
      Steve C. Lamb | I'm your priest, I'm your shrink, I'm your
      PGP Key: 8B6E99C5 | main connection to the switchboard of souls.
      -------------------------------+---------------------------------------------

      Comment

      • Ryan Paul

        #4
        Re: Why a class when there will only be one instance?

        On Wed, 26 May 2004 00:43:49 +0000, SeeBelow wrote:
        [color=blue]
        > I see the value of a class when two or more instances will be created,
        > but Python programmers regularly use a class when there will only be one
        > instance.
        > What is the benefit of this? It has a disadvantage of a whole lot of
        > "self."
        > being required everywhere, making the code less readable. Also, since a
        > strength of Python is rapid application development, it slows one down
        > to have to put in all those self.'s. The code seems much cleaner to me
        > without classes that have only one instance. Oh, also, all the methods
        > of this class will have to have the instance name prepended to them.
        >
        > I would appreciate it if someone could explain the advantages of doing
        > this, or at least the sociological reasons why it occurs.
        >
        > Mitchell Timin[/color]

        defining a class may be useful if you plan on making more instances down
        the line. It's a good OO strategy. I do understand your dislike of 'self'.
        It does seem like clutter. In my code, I shorten it to 's'. In ruby, class
        variables are prefixed with an '@', which makes them easier to discern in
        code, and it is easier than typing 'self'. I wish python had something
        like that. I also think that having to specify the class variable in every
        function definition is a bit silly. Ruby gets rid of that too.

        --SegPhault

        Comment

        • Roy Smith

          #5
          Re: Why a class when there will only be one instance?

          In article <40B3E861.29B03 3D5@shaw.ca>, SeeBelow@SeeBel ow.Nut wrote:
          [color=blue]
          > I see the value of a class when two or more instances will be created,
          > but Python programmers regularly use a class when there will only be one
          > instance.
          > What is the benefit of this? It has a disadvantage of a whole lot of
          > "self."
          > being required everywhere, making the code less readable. Also, since a
          > strength of Python is rapid application development, it slows one down
          > to have to put in all those self.'s. The code seems much cleaner to me
          > without classes that have only one instance. Oh, also, all the methods
          > of this class will have to have the instance name prepended to them.
          >
          > I would appreciate it if someone could explain the advantages of doing
          > this, or at least the sociological reasons why it occurs.
          >
          > Mitchell Timin[/color]

          Typing "self" is a mechanical process which adds very little to the
          development cost. Deciding which things to make classes and which not
          to requires significant mental effort and does add cost. It's just
          easier to make everything a class.

          More than that, most times I've decided to not bother making something a
          class because it was too simple, I've eventually added enough
          functionality to it to change my mind and have to re-do things. That's
          real cost. Much simplier and cheaper to just make it a class from the
          get-go.

          Comment

          • Roy Smith

            #6
            Re: Why a class when there will only be one instance?

            Ryan Paul <segphault@sbcg lobal.net> wrote:[color=blue]
            > defining a class may be useful if you plan on making more instances down
            > the line. It's a good OO strategy. I do understand your dislike of 'self'.
            > It does seem like clutter. In my code, I shorten it to 's'.[/color]

            Please don't do that. While it's true that the first parameter of a
            class method can be named anything, the use of "self" is so
            overwhelmingly ubiquitous it might as well be a standard. Using
            anything else is just going to make your code more difficult for anybody
            else to read and understand.

            Typing is cheap. Thinking is expensive. And, yes Aahz, you can quote
            me on that :-)

            Comment

            • SeeBelow@SeeBelow.Nut

              #7
              Re: Why a class when there will only be one instance?

              Roy Smith wrote:[color=blue]
              >
              > In article <40B3E861.29B03 3D5@shaw.ca>, SeeBelow@SeeBel ow.Nut wrote:
              >[color=green]
              > > I see the value of a class when two or more instances will be created,
              > > but Python programmers regularly use a class when there will only be one
              > > instance.
              > > What is the benefit of this? It has a disadvantage of a whole lot of
              > > "self."
              > > being required everywhere, making the code less readable. Also, since a
              > > strength of Python is rapid application development, it slows one down
              > > to have to put in all those self.'s. The code seems much cleaner to me
              > > without classes that have only one instance. Oh, also, all the methods
              > > of this class will have to have the instance name prepended to them.
              > >
              > > I would appreciate it if someone could explain the advantages of doing
              > > this, or at least the sociological reasons why it occurs.
              > >
              > > Mitchell Timin[/color]
              >
              > Typing "self" is a mechanical process which adds very little to the
              > development cost. Deciding which things to make classes and which not
              > to requires significant mental effort and does add cost. It's just
              > easier to make everything a class.[/color]

              Even easier is not to make anything a class unless there will be two or
              more instances of it. I still don't get what advantage making a class
              buys for you.
              [color=blue]
              >
              > More than that, most times I've decided to not bother making something a
              > class because it was too simple, I've eventually added enough
              > functionality to it to change my mind and have to re-do things. That's
              > real cost. Much simplier and cheaper to just make it a class from the
              > get-go.[/color]

              Why does greater functionality make a class desireable, if there won't
              be multiple instances created?

              Other people have mentioned "code reuse". Again I don't see how a class
              helps to make code reusable. I find methods in a class more difficult
              to reuse than simple function definitions. (unless there are multiple
              instances.)

              Mitchell Timin

              --
              "Many are stubborn in pursuit of the path they have chosen, few in
              pursuit of the goal." - Friedrich Nietzsche

              http://annevolve.sourceforge.net is what I'm into nowadays.
              Humans may write to me at this address: zenguy at shaw dot ca

              Comment

              • Roy Smith

                #8
                Re: Why a class when there will only be one instance?

                In article <40B3F448.EB711 333@shaw.ca>, SeeBelow@SeeBel ow.Nut wrote:
                [color=blue][color=green]
                > > More than that, most times I've decided to not bother making something a
                > > class because it was too simple, I've eventually added enough
                > > functionality to it to change my mind and have to re-do things. That's
                > > real cost. Much simplier and cheaper to just make it a class from the
                > > get-go.[/color]
                >
                > Why does greater functionality make a class desireable, if there won't
                > be multiple instances created?[/color]

                For me, it's more about encapsulation than code re-use. If I've got a
                bunch of functions which operate on a collection of data, to me that
                says "object", which in Python (and most OOPL's) implies "class".

                Bundling it up into a class lets me think about it as a unit. Each
                class is a convenient thought unit for design and testing, and also for
                understanding somebody else's code.

                I don't think there's a need to be dogmatic about it. It's just what I
                find is a natural way to break a program down into smaller pieces you
                can get your brain around one piece at a time.

                Comment

                • SeeBelow@SeeBelow.Nut

                  #9
                  Re: Why a class when there will only be one instance?

                  Roy Smith wrote:[color=blue]
                  >
                  > In article <40B3F448.EB711 333@shaw.ca>, SeeBelow@SeeBel ow.Nut wrote:
                  >[color=green][color=darkred]
                  > > > More than that, most times I've decided to not bother making something a
                  > > > class because it was too simple, I've eventually added enough
                  > > > functionality to it to change my mind and have to re-do things. That's
                  > > > real cost. Much simplier and cheaper to just make it a class from the
                  > > > get-go.[/color]
                  > >
                  > > Why does greater functionality make a class desireable, if there won't
                  > > be multiple instances created?[/color]
                  >
                  > For me, it's more about encapsulation than code re-use. If I've got a
                  > bunch of functions which operate on a collection of data, to me that
                  > says "object", which in Python (and most OOPL's) implies "class".[/color]

                  OK, that makes some sense - to associate certain data and code items and
                  separate them from other code and data. Wouldn't that purpose be served
                  even better by putting them into a different file, and not bother with a
                  class?
                  [color=blue]
                  > Bundling it up into a class lets me think about it as a unit. Each
                  > class is a convenient thought unit for design and testing, and also for
                  > understanding somebody else's code.
                  >
                  > I don't think there's a need to be dogmatic about it. It's just what I
                  > find is a natural way to break a program down into smaller pieces you
                  > can get your brain around one piece at a time.[/color]


                  --
                  "Many are stubborn in pursuit of the path they have chosen, few in
                  pursuit of the goal." - Friedrich Nietzsche

                  http://annevolve.sourceforge.net is what I'm into nowadays.
                  Humans may write to me at this address: zenguy at shaw dot ca

                  Comment

                  • John Roth

                    #10
                    Re: Why a class when there will only be one instance?

                    <SeeBelow@SeeBe low.Nut> wrote in message news:40B3E861.2 9B033D5@shaw.ca ...[color=blue]
                    > I see the value of a class when two or more instances will be created,
                    > but Python programmers regularly use a class when there will only be one
                    > instance.
                    > What is the benefit of this? It has a disadvantage of a whole lot of
                    > "self."
                    > being required everywhere, making the code less readable. Also, since a
                    > strength of Python is rapid application development, it slows one down
                    > to have to put in all those self.'s. The code seems much cleaner to me
                    > without classes that have only one instance. Oh, also, all the methods
                    > of this class will have to have the instance name prepended to them.[/color]

                    I'm only aware of one domain (interactive fiction) where it is really
                    common to have single instance classes. That's served by a number
                    of special purpose languages (Inform, TADS and Hugo spring to
                    mind). Your example (of single use classes in a UI context) seems
                    to be a bit contrived. I've usually found that I could get quite a bit
                    of code reuse out of UI classes simply by thinking the problems
                    through.

                    Granted, single use classes aren't uncommon, but instances of
                    other classes are much more common in most programs. In
                    Python, there are a number of ways of getting singletons without
                    very much extra effort.
                    [color=blue]
                    > I would appreciate it if someone could explain the advantages of doing
                    > this, or at least the sociological reasons why it occurs.[/color]

                    Both have their uses. The class / instance dicotomy comes from
                    the earliest days of object oriented programming, and experiments
                    with classless languages (see the Prothon stuff currently going on,
                    also the IO language) have never been very compelling. A lot of
                    people (which include me) think that it's much to easy to get sloppy
                    with that paradigm.

                    The other thing to understand is that in most OO languages a
                    class is a declaration: it does not appear in the actual object
                    program other than as a framework for the instances that are
                    created. Python is one of the few languages where classes are
                    first class objects.

                    John Roth

                    [color=blue]
                    > Mitchell Timin[/color]


                    Comment

                    • Peter Maas

                      #11
                      Re: Why a class when there will only be one instance?

                      SeeBelow@SeeBel ow.Nut wrote:[color=blue]
                      > Even easier is not to make anything a class unless there will be two or
                      > more instances of it. I still don't get what advantage making a class
                      > buys for you.[/color]

                      To use classes even in single instance cases has some advantages:

                      - There is a unique way of organizing your code.

                      - There is an easy transition to the multiple instance case.

                      - It makes writing meta code (e.g. for documentation, transformation
                      ...) easier.

                      - Code organisation should resemble the real world problem to be modeled
                      (for the sake of clarity): if emphasis is on an entity (e.g. a document)
                      use an object, if emphasis is on activity (e.g. computing the cosine)
                      use a function.

                      These advantages outweigh by far lexical arguments (self. takes soooo long
                      to type) or temporary arguments (instance count is currently = 1).

                      Of course this is largely a matter of taste but I am a 'burnt child'
                      because I had to deal with the one-instance argument in multi-developer
                      projects and found the defender's code quite messy. :)
                      [color=blue]
                      > Other people have mentioned "code reuse". Again I don't see how a class
                      > helps to make code reusable.[/color]

                      Inheritance.
                      [color=blue]
                      > I find methods in a class more difficult to reuse than simple function
                      > definitions. (unless there are multiple instances.)[/color]

                      Why? Methods are functions with an instance argument:

                      class person:
                      def tellName(self):
                      print self.name
                      [color=blue][color=green][color=darkred]
                      >>> p = person()
                      >>> p.name = 'Peter'
                      >>> intro_c = person.tellName
                      >>> intro_c(p)[/color][/color][/color]
                      Peter[color=blue][color=green][color=darkred]
                      >>> intro_i = p.tellName
                      >>> intro_i()[/color][/color][/color]
                      Peter

                      Mit freundlichen Gruessen,

                      Peter Maas

                      --
                      -------------------------------------------------------------------
                      Peter Maas, M+R Infosysteme, D-52070 Aachen, Hubert-Wienen-Str. 24
                      Tel +49-241-93878-0 Fax +49-241-93878-20 eMail peter.maas@mplu sr.de
                      -------------------------------------------------------------------

                      Comment

                      • Ryan Paul

                        #12
                        Re: Why a class when there will only be one instance?

                        On Tue, 25 May 2004 21:20:02 -0400, Roy Smith wrote:
                        [color=blue]
                        > Ryan Paul <segphault@sbcg lobal.net> wrote:[color=green]
                        >> defining a class may be useful if you plan on making more instances down
                        >> the line. It's a good OO strategy. I do understand your dislike of 'self'.
                        >> It does seem like clutter. In my code, I shorten it to 's'.[/color]
                        >
                        > Please don't do that. While it's true that the first parameter of a
                        > class method can be named anything, the use of "self" is so
                        > overwhelmingly ubiquitous it might as well be a standard. Using
                        > anything else is just going to make your code more difficult for anybody
                        > else to read and understand.
                        >
                        > Typing is cheap. Thinking is expensive. And, yes Aahz, you can quote
                        > me on that :-)[/color]

                        I dont conform to a bad standard just because it's a standard. If I did, I
                        would be using java instead of a dynamic language like python. If other
                        people dont like it, thats too bad- they dont have to use my code.

                        Comment

                        • Grant Edwards

                          #13
                          Re: Why a class when there will only be one instance?

                          On 2004-05-26, SeeBelow@SeeBel ow.Nut <SeeBelow@SeeBe low.Nut> wrote:
                          [color=blue]
                          > I see the value of a class when two or more instances will be
                          > created, but Python programmers regularly use a class when
                          > there will only be one instance. What is the benefit of this?[/color]

                          1) Because people are notoriously bad and predicting the
                          future. Their guesses at what "will be" are often wrong.

                          If you use a class, then you can add a second instance
                          later when you figure out what the customer really wanted.

                          2) I find it often helps me think through the problem and
                          arrive at a more structured, easily maintained solution
                          compared to a bunch of random functions.
                          [color=blue]
                          > It has a disadvantage of a whole lot of "self." being required
                          > everywhere, making the code less readable.[/color]

                          In some cases (particulary mathematical computations), the
                          self's can reduce readability. A few local variables can fix
                          that.
                          [color=blue]
                          > Also, since a strength of Python is rapid application
                          > development, it slows one down to have to put in all those
                          > self.'s.[/color]

                          I've never seen any project where the speed of development was
                          limited by typing speed.
                          [color=blue]
                          > The code seems much cleaner to me without classes that have
                          > only one instance.[/color]

                          Until the requirements change (or are actually discovered), and
                          you need more than one instance.

                          --
                          Grant Edwards grante Yow! If I felt any more
                          at SOPHISTICATED I would DIE
                          visi.com of EMBARRASSMENT!

                          Comment

                          • Peter Hickman

                            #14
                            Re: Why a class when there will only be one instance?

                            SeeBelow@SeeBel ow.Nut wrote:[color=blue]
                            > I see the value of a class when two or more instances will be created,
                            > but Python programmers regularly use a class when there will only be one
                            > instance.[/color]

                            This is a lot like putting code in functions. "Its only called in one place, why put it in a function, why not just leave it where it is?" I had a conversation with a dBase
                            programmer many years ago on just these lines and what it came down to was this.

                            for x = 1 to length(orderite ms)
                            calculate_tax(o rderitems[x], .175)
                            next

                            (forgive the pseudo code) is easier to read and the intent of the code is much clearer, and also we had 24 * 80 screens and so you could see much more of the 'flow' of the program
                            than if the code was in lined. Objects just take all that one step further.

                            order.calculate _tax( .175 )

                            Why read fifty lines of code to work out that tax is being calculated for each item in the order when you can read one line? How you achieve this is not all that important, functions
                            or classes only that the flow of the program can be comprehended quickly. Objects don't just make writing code easier but it makes *READING* code easier too, a very important plus
                            (ever had to maintain APL?)

                            Almost everything that I write becomes an object even the programs themselves.

                            Comment

                            • Donald 'Paddy' McCarthy

                              #15
                              Re: Why a class when there will only be one instance?

                              Ryan Paul wrote:[color=blue]
                              > On Tue, 25 May 2004 21:20:02 -0400, Roy Smith wrote:
                              >
                              >[color=green]
                              >>Ryan Paul <segphault@sbcg lobal.net> wrote:
                              >>[color=darkred]
                              >>>defining a class may be useful if you plan on making more instances down
                              >>>the line. It's a good OO strategy. I do understand your dislike of 'self'.
                              >>>It does seem like clutter. In my code, I shorten it to 's'.[/color]
                              >>
                              >>Please don't do that. While it's true that the first parameter of a
                              >>class method can be named anything, the use of "self" is so
                              >>overwhelmingl y ubiquitous it might as well be a standard. Using
                              >>anything else is just going to make your code more difficult for anybody
                              >>else to read and understand.
                              >>
                              >>Typing is cheap. Thinking is expensive. And, yes Aahz, you can quote
                              >>me on that :-)[/color]
                              >
                              >
                              > I dont conform to a bad standard just because it's a standard. If I did, I
                              > would be using java instead of a dynamic language like python. If other
                              > people dont like it, thats too bad- they dont have to use my code.
                              >[/color]
                              It's your code but might I suggest that you then be consistent in your
                              naming convention to aid re-use?
                              - Pad.

                              Comment

                              Working...