python's OOP question

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

    #16
    Re: python's OOP question



    On Oct 16, 9:01 pm, Bruno Desthuilliers <o...@xiludom.g rowrote:
    neoedmund wrote:
    Bruno Desthuilliers wrote:
    neoedmund wrote:
    (*PLEASE* stop top-posting - corrected)
    >Ben Finney wrote:
    >>[Please don't top-post above the text to which you're replying.]
    >
    >>"neoedmund" <neoedm...@gmai l.comwrites:
    >
    >>>I'm trying to achieve a higher level of "reusabilit y". Maybe it
    >>>cannot be done in python? Can anybody help me?
    >>What, specifically, are you trying to achieve? What problem needs
    >>solving?
    >python use multiple inheritance.
    >but "inheritanc e" means you must inherite all methods from super type.
    >now i just need "some" methods from one type and "some" methods from
    >other types, to build the new type.
    >Do you think this way is more flexible than tranditional inheritance?
    >
    While dynamically adding attributes (and methods - which are attributes
    too) is not a problem, I'd second Gregor's anwser : it might be better
    to use composition/delegation here.
    >
    Could you show some code to help me know how composition/delegation can
    be done here? Thanks.About composition/delegation, there's no "one-size-fits-all" answer, but
    the main idea is to use the magic '__getattr__(se lf, name)' method.
    >
    Now back to your specific case : after a better reading of your original
    question, straight composition/delegation wouldn't work here - at least
    not without modifications to both C1 and C2 (sorry, should have read
    better the first time).
    >
    Given the context (ie : "create a new type with methods from type X and
    methods from type Y"), a very simple solution could be:
    >
    class C3(object):
    m = C2.m.im_func
    v = C1.v.im_func
    >
    FWIW, if you have full control over C1, C2 and C3, you could also just
    'externalize' the functions definitions:
    >
    def v1(self, o):
    return "expected "+o
    >
    def v2(self, o):
    return "unexpected "+o
    >
    def m2(self):
    """ requires that 'self' has a v(self, somestring) method """
    print self.v("aaa")
    >
    class C1(object):
    v = v1
    >
    class C2(object):
    v = v2
    m = m2
    >
    class C3(object):
    v = v1
    m = m2
    >
    The problem (with the whole approach, whatever the choosen technical
    solution) is that if one of theses methods depends on another one (or on
    any other attribute) that is not defined in your new class, you're in
    trouble. This is not such a big deal in the above example, but might
    become much more brittle in real life.
    >
    Now we can look at the problem from a different perspective. You wrote:
    """
    but "inheritanc e" means you must inherite all methods from super type.
    now i just need "some" methods from one type and "some" methods from
    other types, to build the new type.
    """
    >
    What is your problem with having the other extra methods too ?
    >
    --
    bruno desthuilliers
    python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
    p in 'on...@xiludom. gro'.split('@')])"
    Bruno , your 2 great and clear samples revealed what method is in
    python, which is also I really want to ask. thank you.
    So I can reuse a method freely only if it's worth reusing.
    For the word "inheritanc e", in some aspect, meanings reuse the super
    class, with the condition: must reuse everything from super class.
    It's lack of a option to select which methods are to be reused.
    this is something should be improved for general OOP i think.
    So to answer " What is your problem with having the other extra methods
    too ?",
    in real life, a class is not defined so well that any method is needed
    by sub-class. so contains a method never to be used is meaningless and
    i think something should forbidden.
    also, any handy methods in a class can be grabbed out for our reuse. so
    we can forgotting the bundering inheritance tree and order.

    Comment

    • Bruno Desthuilliers

      #17
      Re: python's OOP question

      neoedmund wrote:
      (snip)
      So I can reuse a method freely only if it's worth reusing.
      For the word "inheritanc e", in some aspect, meanings reuse the super
      class, with the condition: must reuse everything from super class.
      Not really. In fact, inheritance *is* a special case of
      composition/delegation. A 'child' class is a class that has references
      to other classes - it's 'parents' -, and then attributes that are not
      found in the instance or child class are looked up in the parents
      (according to mro rules in case of multiple inheritance). And that's all
      there is.
      It's lack of a option to select which methods are to be reused.
      Methods not redefined in the 'child' class or it's instance are
      'reusable'. Now they are only effectively 'reused' if and when called by
      client code. So the 'option to select which methods are to be reused' is
      mostly up to both the 'child' class and code using it.
      this is something should be improved for general OOP i think.
      So to answer " What is your problem with having the other extra methods
      too ?",
      in real life, a class is not defined so well that any method is needed
      by sub-class.
      Then perhaps is it time to refactor. A class should be a highly cohesive
      unit. If you find yourself needing only a specific subset of a class, it
      may be time to extract this subset in it's own class. Given Python's
      support for both multiple inheritance and composition/delegation, it's
      usually a trivial task (unless you already mixed up too many orthogonal
      concerns in your base class...).

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

      Comment

      • neoedmund

        #18
        Re: python's OOP question


        Bruno Desthuilliers wrote:
        neoedmund wrote:
        (snip)
        So I can reuse a method freely only if it's worth reusing.
        For the word "inheritanc e", in some aspect, meanings reuse the super
        class, with the condition: must reuse everything from super class.
        >
        Not really. In fact, inheritance *is* a special case of
        composition/delegation. A 'child' class is a class that has references
        to other classes - it's 'parents' -, and then attributes that are not
        found in the instance or child class are looked up in the parents
        (according to mro rules in case of multiple inheritance). And that's all
        there is.
        >
        It's lack of a option to select which methods are to be reused.
        >
        Methods not redefined in the 'child' class or it's instance are
        'reusable'. Now they are only effectively 'reused' if and when called by
        client code. So the 'option to select which methods are to be reused' is
        mostly up to both the 'child' class and code using it.
        >
        this is something should be improved for general OOP i think.
        So to answer " What is your problem with having the other extra methods
        too ?",
        in real life, a class is not defined so well that any method is needed
        by sub-class.
        >
        Then perhaps is it time to refactor. A class should be a highly cohesive
        unit. If you find yourself needing only a specific subset of a class, it
        may be time to extract this subset in it's own class. Given Python's
        support for both multiple inheritance and composition/delegation, it's
        usually a trivial task (unless you already mixed up too many orthogonal
        concerns in your base class...).
        >
        --
        bruno desthuilliers
        python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
        p in 'onurb@xiludom. gro'.split('@')])"
        ivestgating the web, i found something similiar with my approch:

        "Duck-typing avoids tests using type() or isinstance(). Instead, it
        typically employs hasattr() tests"

        Comment

        • neoedmund

          #19
          Re: python's OOP question


          Bruno Desthuilliers wrote:
          neoedmund wrote:
          (snip)
          So I can reuse a method freely only if it's worth reusing.
          For the word "inheritanc e", in some aspect, meanings reuse the super
          class, with the condition: must reuse everything from super class.
          >
          Not really. In fact, inheritance *is* a special case of
          composition/delegation. A 'child' class is a class that has references
          to other classes - it's 'parents' -, and then attributes that are not
          found in the instance or child class are looked up in the parents
          (according to mro rules in case of multiple inheritance). And that's all
          there is.
          >
          It's lack of a option to select which methods are to be reused.
          >
          Methods not redefined in the 'child' class or it's instance are
          'reusable'. Now they are only effectively 'reused' if and when called by
          client code. So the 'option to select which methods are to be reused' is
          mostly up to both the 'child' class and code using it.
          >
          this is something should be improved for general OOP i think.
          So to answer " What is your problem with having the other extra methods
          too ?",
          in real life, a class is not defined so well that any method is needed
          by sub-class.
          >
          Then perhaps is it time to refactor. A class should be a highly cohesive
          unit. If you find yourself needing only a specific subset of a class, it
          may be time to extract this subset in it's own class. Given Python's
          support for both multiple inheritance and composition/delegation, it's
          usually a trivial task (unless you already mixed up too many orthogonal
          concerns in your base class...).
          >
          --
          bruno desthuilliers
          python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
          p in 'onurb@xiludom. gro'.split('@')])"
          I donnot agree with your "it's time to refactory" very much, man has
          probly never has time to do such things. suppose a system is working
          soundly, you maybe has no time or motivation to do refactory instead of
          having a vocation to a island. it's easy to say, at lease myself has
          not the experience to do such things, :-)

          Comment

          • Fredrik Lundh

            #20
            Re: python's OOP question

            neoedmund wrote:
            ivestgating the web, i found something similiar with my approch:

            "Duck-typing avoids tests using type() or isinstance(). Instead, it
            typically employs hasattr() tests"
            that's not entirely correct, though: in Python, duck-typing typically
            uses "Easier to Ask Forgiveness than Permission" (EAFP), aka "Just Do
            It", rather than "Look Before You Leap" (LBYL).

            </F>

            Comment

            • Ben Finney

              #21
              Re: python's OOP question

              "neoedmund" <neoedmund@gmai l.comwrites:
              Bruno Desthuilliers wrote:
              neoedmund wrote:
              in real life, a class is not defined so well that any method is
              needed by sub-class.
              Then perhaps is it time to refactor. A class should be a highly
              cohesive unit. If you find yourself needing only a specific subset
              of a class, it may be time to extract this subset in it's own
              class.
              >
              I donnot agree with your "it's time to refactory" very much, man has
              probly never has time to do such things.
              I respectfully suggest that the *reason* you find yourself with little
              time to refactor is probably related to the fact that you *need* to
              refactor. If your code is crufty and poorly-designed, it is costing
              you every time you need to maintain it.

              Would it help if we called it "preventati ve maintenance"?

              --
              \ "I bought a dog the other day. I named him Stay. It's fun to |
              `\ call him. 'Come here, Stay! Come here, Stay!' He went insane. |
              _o__) Now he just ignores me and keeps typing." -- Steven Wright |
              Ben Finney

              Comment

              • Neil Cerutti

                #22
                Re: python's OOP question

                On 2006-10-18, neoedmund <neoedmund@gmai l.comwrote:
                ivestgating the web, i found something similiar with my approch:

                "Duck-typing avoids tests using type() or isinstance(). Instead, it
                typically employs hasattr() tests"
                It's pity it didn't get called quack typing. One ckecks if
                some unknown noun can quack, not if a duck can do something
                unknown.

                --
                Neil Cerutti

                Comment

                • Peter Otten

                  #23
                  Re: python's OOP question

                  Neil Cerutti wrote:
                  On 2006-10-18, neoedmund <neoedmund@gmai l.comwrote:
                  >ivestgating the web, i found something similiar with my approch:
                  >http://en.wikipedia.org/wiki/Duck_typing
                  >"Duck-typing avoids tests using type() or isinstance(). Instead, it
                  >typically employs hasattr() tests"
                  >
                  It's pity it didn't get called quack typing.
                  That's because the quacks recommend static typing.

                  Peter

                  Comment

                  • Peter Otten

                    #24
                    Re: python's OOP question

                    Neil Cerutti wrote:
                    On 2006-10-18, neoedmund <neoedmund@gmai l.comwrote:
                    >ivestgating the web, i found something similiar with my approch:
                    >http://en.wikipedia.org/wiki/Duck_typing
                    >"Duck-typing avoids tests using type() or isinstance(). Instead, it
                    >typically employs hasattr() tests"
                    >
                    It's pity it didn't get called quack typing.
                    That's because the quacks prescribe static typing.

                    Peter

                    Comment

                    Working...