refering to base classes

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

    #1

    refering to base classes

    hi - Im quite new to python, wondering if anyone can help me understand
    something about inheritance here. In this trivial example, how could I
    modify the voice method of 'dog' to call the base class 'creatures'
    voice method from with in it?

    class creature:
    def __init__(self):
    self.noise=""
    def voice(self):
    return "voice:" + self.noise

    class dog(creature):
    def __init__(self):
    self.noise="bar k"

    def voice(self):
    print "brace your self:"

    thanks
    glenn

  • Chaz Ginger

    #2
    Re: refering to base classes

    glenn wrote:
    hi - Im quite new to python, wondering if anyone can help me understand
    something about inheritance here. In this trivial example, how could I
    modify the voice method of 'dog' to call the base class 'creatures'
    voice method from with in it?
    >
    class creature:
    def __init__(self):
    self.noise=""
    def voice(self):
    return "voice:" + self.noise
    >
    class dog(creature):
    def __init__(self):
    self.noise="bar k"
    >
    def voice(self):
    print "brace your self:"
    >
    thanks
    glenn
    >
    Try this:

    class dog(creature):
    .....
    def voice(self):
    print "brace your self:"
    creature.voice( self)

    This should do it.

    Comment

    • Chaz Ginger

      #3
      Re: refering to base classes

      Chaz Ginger wrote:
      glenn wrote:
      >hi - Im quite new to python, wondering if anyone can help me understand
      >something about inheritance here. In this trivial example, how could I
      >modify the voice method of 'dog' to call the base class 'creatures'
      >voice method from with in it?
      >>
      >class creature:
      > def __init__(self):
      > self.noise=""
      > def voice(self):
      > return "voice:" + self.noise
      >>
      >class dog(creature):
      > def __init__(self):
      > self.noise="bar k"
      >>
      > def voice(self):
      > print "brace your self:"
      >>
      >thanks
      >glenn
      >>
      Try this:
      >
      class dog(creature):
      .....
      def voice(self):
      print "brace your self:"
      creature.voice( self)
      >
      This should do it.
      I did forget to mention that in 'dog"s' __init__ you had better call
      creature's __init__. You might make it look like this:

      def __init__(self):
      self.noise = 'bark'
      creature.__init __(self)

      There is another approach - using Superclass - but I will leave that
      exercise to the reader.

      Comment

      • Roberto Bonvallet

        #4
        Re: refering to base classes

        glenn wrote:
        [...] In this trivial example, how could I modify the voice method of
        'dog' to call the base class 'creatures' voice method from with in it?
        >
        class creature:
        def __init__(self):
        self.noise=""
        def voice(self):
        return "voice:" + self.noise
        >
        class dog(creature):
        def __init__(self):
        self.noise="bar k"
        >
        def voice(self):
        print "brace your self:"
        If you want dog.voice() to just print "voice: bark", you just have to omit
        the voice method for the dog class: it will be inherited from creature.

        If you want dog.voice() to do something else, you can call superclass'
        method like this:

        def voice(self):
        creature.voice( self)
        print "brace your self"
        any_other_magic ()

        HTH
        --
        Roberto Bonvallet

        Comment

        • Bruno Desthuilliers

          #5
          Re: refering to base classes

          glenn wrote:
          hi - Im quite new to python, wondering if anyone can help me understand
          something about inheritance here. In this trivial example, how could I
          modify the voice method of 'dog' to call the base class 'creatures'
          voice method from with in it?
          >
          class creature:
          def __init__(self):
          self.noise=""
          def voice(self):
          return "voice:" + self.noise
          >
          class dog(creature):
          def __init__(self):
          self.noise="bar k"
          >
          def voice(self):
          print "brace your self:"

          <ot>
          It might be better to use newstyle classes if you can. Also, the
          convention is to use CamelCase for classes names (unless you have a
          strong reason to do otherwise).
          </ot>

          Here you could use a class attribute to provide a default:

          class Creature(object ):
          noise = ""

          def voice(self):
          return "voice:" + self.noise


          class Dog(Creature):
          noise="bark"

          def voice(self):
          print "brace your self:"
          return Creature.voice( self)
          # can also use this instead, cf the Fine Manual
          return super(Dog, self).voice()

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

          Comment

          • Jason

            #6
            Re: refering to base classes

            Chaz Ginger wrote:
            Chaz Ginger wrote:
            glenn wrote:
            hi - Im quite new to python, wondering if anyone can help me understand
            something about inheritance here. In this trivial example, how could I
            modify the voice method of 'dog' to call the base class 'creatures'
            voice method from with in it?
            >
            class creature:
            def __init__(self):
            self.noise=""
            def voice(self):
            return "voice:" + self.noise
            >
            class dog(creature):
            def __init__(self):
            self.noise="bar k"
            >
            def voice(self):
            print "brace your self:"
            >
            I did forget to mention that in 'dog"s' __init__ you had better call
            creature's __init__. You might make it look like this:
            >
            def __init__(self):
            self.noise = 'bark'
            creature.__init __(self)
            >
            There's a problem with Chaz's __init__() method. Notice that the
            creature class's __init__ sets self.noise to the empty string. In this
            case, the superclass's __init__() method should be called first:

            class dog(creature):
            def __init__(self):
            creature.__init __(self)
            self.noise = "bark"
            def voice(self):
            print "brace your self:"
            creature.voice( self)

            --Jason

            Comment

            • Chaz Ginger

              #7
              Re: refering to base classes

              Jason wrote:
              Chaz Ginger wrote:
              >Chaz Ginger wrote:
              >>glenn wrote:
              >>>hi - Im quite new to python, wondering if anyone can help me understand
              >>>something about inheritance here. In this trivial example, how could I
              >>>modify the voice method of 'dog' to call the base class 'creatures'
              >>>voice method from with in it?
              >>>>
              >>>class creature:
              >>> def __init__(self):
              >>> self.noise=""
              >>> def voice(self):
              >>> return "voice:" + self.noise
              >>>>
              >>>class dog(creature):
              >>> def __init__(self):
              >>> self.noise="bar k"
              >>>>
              >>> def voice(self):
              >>> print "brace your self:"
              >I did forget to mention that in 'dog"s' __init__ you had better call
              >creature's __init__. You might make it look like this:
              >>
              >def __init__(self):
              > self.noise = 'bark'
              > creature.__init __(self)
              >>
              >
              There's a problem with Chaz's __init__() method. Notice that the
              creature class's __init__ sets self.noise to the empty string. In this
              case, the superclass's __init__() method should be called first:
              >
              class dog(creature):
              def __init__(self):
              creature.__init __(self)
              self.noise = "bark"
              def voice(self):
              print "brace your self:"
              creature.voice( self)
              >
              --Jason
              >
              Very true....I was showing him in "spirit only"...lol.


              Chaz.

              Comment

              • glenn

                #8
                Re: refering to base classes


                Chaz Ginger wrote:
                Chaz Ginger wrote:
                glenn wrote:
                hi - Im quite new to python, wondering if anyone can help me understand
                something about inheritance here. In this trivial example, how could I
                modify the voice method of 'dog' to call the base class 'creatures'
                voice method from with in it?
                >
                class creature:
                def __init__(self):
                self.noise=""
                def voice(self):
                return "voice:" + self.noise
                >
                class dog(creature):
                def __init__(self):
                self.noise="bar k"
                >
                def voice(self):
                print "brace your self:"
                >
                thanks
                glenn
                >
                Try this:

                class dog(creature):
                .....
                def voice(self):
                print "brace your self:"
                creature.voice( self)

                This should do it.
                I did forget to mention that in 'dog"s' __init__ you had better call
                creature's __init__. You might make it look like this:
                >
                def __init__(self):
                self.noise = 'bark'
                creature.__init __(self)
                >
                There is another approach - using Superclass - but I will leave that
                exercise to the reader.
                first tip worked - funny thing was I =thought= I done that, but clearly
                not - so thanks was going mad.
                Superclass?... ok will look into this
                thanks for reply(s)
                Glenn

                Comment

                • glenn

                  #9
                  Re: refering to base classes


                  Bruno Desthuilliers wrote:
                  glenn wrote:
                  hi - Im quite new to python, wondering if anyone can help me understand
                  something about inheritance here. In this trivial example, how could I
                  modify the voice method of 'dog' to call the base class 'creatures'
                  voice method from with in it?

                  class creature:
                  def __init__(self):
                  self.noise=""
                  def voice(self):
                  return "voice:" + self.noise

                  class dog(creature):
                  def __init__(self):
                  self.noise="bar k"

                  def voice(self):
                  print "brace your self:"
                  >
                  >
                  <ot>
                  It might be better to use newstyle classes if you can. Also, the
                  convention is to use CamelCase for classes names (unless you have a
                  strong reason to do otherwise).
                  </ot>
                  >
                  Here you could use a class attribute to provide a default:
                  >
                  class Creature(object ):
                  noise = ""
                  >
                  def voice(self):
                  return "voice:" + self.noise
                  >
                  >
                  class Dog(Creature):
                  noise="bark"
                  >
                  def voice(self):
                  print "brace your self:"
                  return Creature.voice( self)
                  # can also use this instead, cf the Fine Manual
                  return super(Dog, self).voice()
                  >
                  My 2 cents
                  ohh - interesting. Thanks for the camelCase tip - dont have a good
                  reason to do otherwise, just bad habits.
                  so for your $.02 do you see this as being, umm, superior in anyway to
                  creature.voice( )?

                  glenn

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

                  Comment

                  • glenn

                    #10
                    Re: refering to base classes

                    Hi Roberto
                    If you want dog.voice() to just print "voice: bark", you just have to omit
                    the voice method for the dog class: it will be inherited from creature.
                    >
                    I would have thought this would be correct, but in this case, plus in
                    others im playin with, I get this issue:
                    -----------------------
                    given animal.py is:
                    class creature:
                    def __init__(self):
                    self.noise=""
                    def voice(self):
                    return "voice:" + self.noise

                    class dog(creature):
                    def __init__(self):
                    self.noise="bar k"

                    then I get this outcome...
                    >>>import animal
                    >>beagle=animal .dog
                    >>beagle.voice( )
                    Traceback (most recent call last):
                    File "<input>", line 1, in ?
                    TypeError: unbound method voice() must be called with dog instance as
                    first argument (got nothing instead)
                    >>>
                    ------------------------
                    So I guess it wants something in position of self?

                    any idea what Im doing wrong? - this would be very handy as its a point
                    Im stymied on a couple of 'projects'
                    thanks
                    Glenn
                    If you want dog.voice() to do something else, you can call superclass'
                    method like this:
                    >
                    def voice(self):
                    creature.voice( self)
                    print "brace your self"
                    any_other_magic ()
                    >
                    HTH
                    --
                    Roberto Bonvallet

                    Comment

                    • Ben Finney

                      #11
                      Naming conventions (was: Re: refering to base classes)

                      "glenn" <glenn@tangelos oftware.netwrit es:
                      Bruno Desthuilliers wrote:
                      It might be better to use newstyle classes if you can. Also, the
                      convention is to use CamelCase for classes names (unless you have
                      a strong reason to do otherwise).
                      Note that this style is more correctly called TitleCase, since the
                      first letter of *every* word is capitalised, like in a headline (or
                      title). "camel case" is different -- see below.
                      ohh - interesting. Thanks for the camelCase tip - dont have a good
                      reason to do otherwise, just bad habits.
                      The style called camelCase (all words run together, capitalise first
                      letter of every word except the first) is prevalent in Java, where it
                      denotes the name of an *instance*, in contrast to a *class* which is
                      named with TitleCase.

                      The camelCase style is less popular in the Python world, where (as per
                      PEP 8) instances are named with all lower case, either joinedwords or
                      separate_by_und erscores.

                      --
                      \ "Crime is contagious ... if the government becomes a |
                      `\ lawbreaker, it breeds contempt for the law." -- Justice Louis |
                      _o__) Brandeis |
                      Ben Finney

                      Comment

                      • Steve Holden

                        #12
                        Re: refering to base classes

                        glenn wrote:
                        Hi Roberto
                        >
                        >>If you want dog.voice() to just print "voice: bark", you just have to omit
                        >>the voice method for the dog class: it will be inherited from creature.
                        >>
                        >
                        I would have thought this would be correct, but in this case, plus in
                        others im playin with, I get this issue:
                        -----------------------
                        given animal.py is:
                        class creature:
                        def __init__(self):
                        self.noise=""
                        def voice(self):
                        return "voice:" + self.noise
                        >
                        class dog(creature):
                        def __init__(self):
                        self.noise="bar k"
                        >
                        then I get this outcome...
                        >
                        >>>>import animal
                        >>>>beagle=anim al.dog
                        [...]

                        Shouldn't that be

                        beagle = animal.dog()

                        to create an instance?

                        We've all done it ...

                        regards
                        Steve
                        --
                        Steve Holden +44 150 684 7255 +1 800 494 3119
                        Holden Web LLC/Ltd http://www.holdenweb.com
                        Skype: holdenweb http://holdenweb.blogspot.com
                        Recent Ramblings http://del.icio.us/steve.holden

                        Comment

                        • Bruno Desthuilliers

                          #13
                          Re: Naming conventions

                          Ben Finney wrote:
                          "glenn" <glenn@tangelos oftware.netwrit es:
                          >
                          >Bruno Desthuilliers wrote:
                          >>It might be better to use newstyle classes if you can. Also, the
                          >>convention is to use CamelCase for classes names (unless you have
                          >>a strong reason to do otherwise).
                          >
                          Note that this style is more correctly called TitleCase, since the
                          first letter of *every* word is capitalised, like in a headline (or
                          title). "camel case" is different -- see below.
                          Oops, sorry - my mistake.

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

                          Comment

                          • Bruno Desthuilliers

                            #14
                            Re: refering to base classes

                            glenn wrote:
                            Bruno Desthuilliers wrote:
                            >
                            (snip)
                            >>
                            >Here you could use a class attribute to provide a default:
                            >>
                            >class Creature(object ):
                            > noise = ""
                            >>
                            > def voice(self):
                            > return "voice:" + self.noise
                            >>
                            >>
                            >class Dog(Creature):
                            > noise="bark"
                            >>
                            > def voice(self):
                            > print "brace your self:"
                            > return Creature.voice( self)
                            > # can also use this instead, cf the Fine Manual
                            > return super(Dog, self).voice()
                            >>
                            (snip)
                            so for your $.02 do you see this as being, umm, superior in anyway to
                            creature.voice( )?
                            I suppose "this" refers to the use of super() ? If so, I wouldn't say
                            it's "superior", but it can be helpful with complex inheritence scheme
                            (something that does'nt happen very frequently in Python), and more
                            specifically with multiple inheritance. You may want to read this for
                            more infos:



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

                            Comment

                            • glenn

                              #15
                              Re: refering to base classes

                              >
                              Shouldn't that be
                              >
                              beagle = animal.dog()
                              >
                              to create an instance?
                              >
                              We've all done it ...
                              lol - actually Im confused about this - there seem to be cases where
                              instantiaing with:
                              instance=module .classname()
                              gives me an error, but
                              instance=module .classname
                              doesnt - so I got into that habit, except for where I had a constructor
                              with parameters - except now Im feeling foolish because I cant
                              replicate the error - which suggests I didnt understand the error
                              message properly in the first place... arrgh
                              I guess thats just part of the process of gaining a new language.

                              glenn

                              Comment

                              Working...