[TriPython] Let's talk about classes: invoking class methods with the class name

Jeff Heard jefferson.r.heard at gmail.com
Fri Nov 4 11:09:34 EDT 2016


Also, yes, as Calvin said. If you had inherited from one or multiple
classes, this lets you control what super-class's method implementation you
call, usually within your class implementation.

class C(A, B):
  # use the method defined in B, rather than leave it up to the interpreter
default.
  def method_defined_in_a_and_b(self, *args, **kwargs):
     return B.method_defined_in_a_and_b(self, *args, **kwargs)

On Thu, Nov 3, 2016 at 11:18 PM, Calvin Spealman <ironfroggy at gmail.com>
wrote:

>    Depending on the context, and based on the code style (it looks like a
>    fairly older style) this is a pattern that I used to see to invoke a
>    method on a *specific* class of an instance which might have more than
> one
>    class through inheritance.
>    On Thu, Nov 3, 2016 at 9:57 PM, Jeff Heard
>    <[1]jefferson.r.heard at gmail.com> wrote:
>
>         I would call it "message passing style" personally, but I'm not
> sure
>      it
>         has a name.  similar though to the way Smalltalk or Common Lisp's
>      methods
>         work.  I don't think it's "good python style" but you *could*
>      technically
>         pass an object that is not of type LineSeg, not even related, and
> as
>      along
>         as it "walks like a duck and quacks like a duck it would ... um,
> swim
>      like
>         a duck.
>         On Thu, Nov 3, 2016 at 9:35 PM, Laura Tateosian
>      <[1][2]lgtateos at ncsu.edu>
>         wrote:
>
>              Hello all,
>              I would like to hear your take on this. Say you have defined a
>      class
>           and
>              instantiated an instance as below.
>
>              class LineSeg:
>                  def __init__(self, x1, y1, x2, y2):
>                      self.x1 = x1
>                      self.y1 = y1
>                      self.x2 = x2
>                      self.y2 = y2
>                  def printSegment(self):
>                      print 'Endpoint 1:( {0}, {1}
> )'.format(self.x1,self.y1)
>                      print 'Endpoint 2:( {0}, {1}
> )'.format(self.x2,self.y2)
>              theSeg = LineSeg(1,2,3,4)
>              Then suppose you then invoke the printSegment method like
>      below...
>              LineSeg.printSegment(theSeg)
>              This line is not what I would expect.   What is this approach
>           called?  Why
>              would you want to do this?  It works.  I can kind of see why
> it
>           works, but
>              what are the pros/cons as compared to the standard approach
> like
>           below?
>              theSeg.printSegment()
>
>           _______________________________________________
>           TriZPUG mailing list
>           [2][3]TriZPUG at python.org
>           [3][4]https://mail.python.org/mailman/listinfo/trizpug
>           [4][5]http://tripython.org is the Triangle Python Users Group
>
>      References
>
>         Visible links
>         1. mailto:[6]lgtateos at ncsu.edu
>         2. mailto:[7]TriZPUG at python.org
>         3. [8]https://mail.python.org/mailman/listinfo/trizpug
>         4. [9]http://tripython.org/
>
>      _______________________________________________
>      TriZPUG mailing list
>      [10]TriZPUG at python.org
>      [11]https://mail.python.org/mailman/listinfo/trizpug
>      [12]http://tripython.org is the Triangle Python Users Group
>
>    --
>    Read my blog! I depend on your acceptance of my opinion! I am
> interesting!
>    [13]http://hub.ironfroggy.com/
>    Follow me if you're into that sort of thing:
>    [14]http://www.twitter.com/ironfroggy
>
> References
>
>    Visible links
>    1. mailto:jefferson.r.heard at gmail.com
>    2. mailto:lgtateos at ncsu.edu
>    3. mailto:TriZPUG at python.org
>    4. https://mail.python.org/mailman/listinfo/trizpug
>    5. http://tripython.org/
>    6. mailto:lgtateos at ncsu.edu
>    7. mailto:TriZPUG at python.org
>    8. https://mail.python.org/mailman/listinfo/trizpug
>    9. http://tripython.org/
>   10. mailto:TriZPUG at python.org
>   11. https://mail.python.org/mailman/listinfo/trizpug
>   12. http://tripython.org/
>   13. http://hub.ironfroggy.com/
>   14. http://www.twitter.com/ironfroggy
>
> _______________________________________________
> TriZPUG mailing list
> TriZPUG at python.org
> https://mail.python.org/mailman/listinfo/trizpug
> http://tripython.org is the Triangle Python Users Group
>
-------------- next part --------------
   Also, yes, as Calvin said. If you had inherited from one or multiple
   classes, this lets you control what super-class's method implementation
   you call, usually within your class implementation.
   class C(A, B):
     # use the method defined in B, rather than leave it up to the
   interpreter default.
     def method_defined_in_a_and_b(self, *args, **kwargs):
        return B.method_defined_in_a_and_b(self, *args, **kwargs)
   On Thu, Nov 3, 2016 at 11:18 PM, Calvin Spealman <[1]ironfroggy at gmail.com>
   wrote:

        Depending on the context, and based on the code style (it looks like
     a
        fairly older style) this is a pattern that I used to see to invoke a
        method on a *specific* class of an instance which might have more
     than one
        class through inheritance.
        On Thu, Nov 3, 2016 at 9:57 PM, Jeff Heard
        <[1][2]jefferson.r.heard at gmail.com> wrote:

             I would call it "message passing style" personally, but I'm not
     sure
          it
             has a name.  similar though to the way Smalltalk or Common
     Lisp's
          methods
             work.  I don't think it's "good python style" but you *could*
          technically
             pass an object that is not of type LineSeg, not even related,
     and as
          along
             as it "walks like a duck and quacks like a duck it would ... um,
     swim
          like
             a duck.
             On Thu, Nov 3, 2016 at 9:35 PM, Laura Tateosian
          <[1][2][3]lgtateos at ncsu.edu>
             wrote:

                  Hello all,
                  I would like to hear your take on this. Say you have
     defined a
          class
               and
                  instantiated an instance as below.
                    
                  class LineSeg:
                      def __init__(self, x1, y1, x2, y2):
                          self.x1 = x1
                          self.y1 = y1
                          self.x2 = x2
                          self.y2 = y2
                      def printSegment(self):
                          print 'Endpoint 1:( {0}, {1}
     )'.format(self.x1,self.y1)
                          print 'Endpoint 2:( {0}, {1}
     )'.format(self.x2,self.y2)
                  theSeg = LineSeg(1,2,3,4)
                  Then suppose you then invoke the printSegment method like
          below...
                  LineSeg.printSegment(theSeg)
                  This line is not what I would expect.   What is this
     approach
               called?  Why
                  would you want to do this?  It works.  I can kind of see
     why it
               works, but
                  what are the pros/cons as compared to the standard approach
     like
               below?
                  theSeg.printSegment()

               _______________________________________________
               TriZPUG mailing list
               [2][3][4]TriZPUG at python.org
               [3][4][5]https://mail.python.org/mailman/listinfo/trizpug
               [4][5][6]http://tripython.org is the Triangle Python Users
     Group

          References

             Visible links
             1. mailto:[6][7]lgtateos at ncsu.edu
             2. mailto:[7][8]TriZPUG at python.org
             3. [8][9]https://mail.python.org/mailman/listinfo/trizpug
             4. [9][10]http://tripython.org/

          _______________________________________________
          TriZPUG mailing list
          [10][11]TriZPUG at python.org
          [11][12]https://mail.python.org/mailman/listinfo/trizpug
          [12][13]http://tripython.org is the Triangle Python Users Group

        --
        Read my blog! I depend on your acceptance of my opinion! I am
     interesting!
        [13][14]http://hub.ironfroggy.com/
        Follow me if you're into that sort of thing:
        [14][15]http://www.twitter.com/ironfroggy

     References

        Visible links
        1. mailto:[16]jefferson.r.heard at gmail.com
        2. mailto:[17]lgtateos at ncsu.edu
        3. mailto:[18]TriZPUG at python.org
        4. [19]https://mail.python.org/mailman/listinfo/trizpug
        5. [20]http://tripython.org/
        6. mailto:[21]lgtateos at ncsu.edu
        7. mailto:[22]TriZPUG at python.org
        8. [23]https://mail.python.org/mailman/listinfo/trizpug
        9. [24]http://tripython.org/
       10. mailto:[25]TriZPUG at python.org
       11. [26]https://mail.python.org/mailman/listinfo/trizpug
       12. [27]http://tripython.org/
       13. [28]http://hub.ironfroggy.com/
       14. [29]http://www.twitter.com/ironfroggy

     _______________________________________________
     TriZPUG mailing list
     [30]TriZPUG at python.org
     [31]https://mail.python.org/mailman/listinfo/trizpug
     [32]http://tripython.org is the Triangle Python Users Group

References

   Visible links
   1. mailto:ironfroggy at gmail.com
   2. mailto:jefferson.r.heard at gmail.com
   3. mailto:lgtateos at ncsu.edu
   4. mailto:TriZPUG at python.org
   5. https://mail.python.org/mailman/listinfo/trizpug
   6. http://tripython.org/
   7. mailto:lgtateos at ncsu.edu
   8. mailto:TriZPUG at python.org
   9. https://mail.python.org/mailman/listinfo/trizpug
  10. http://tripython.org/
  11. mailto:TriZPUG at python.org
  12. https://mail.python.org/mailman/listinfo/trizpug
  13. http://tripython.org/
  14. http://hub.ironfroggy.com/
  15. http://www.twitter.com/ironfroggy
  16. mailto:jefferson.r.heard at gmail.com
  17. mailto:lgtateos at ncsu.edu
  18. mailto:TriZPUG at python.org
  19. https://mail.python.org/mailman/listinfo/trizpug
  20. http://tripython.org/
  21. mailto:lgtateos at ncsu.edu
  22. mailto:TriZPUG at python.org
  23. https://mail.python.org/mailman/listinfo/trizpug
  24. http://tripython.org/
  25. mailto:TriZPUG at python.org
  26. https://mail.python.org/mailman/listinfo/trizpug
  27. http://tripython.org/
  28. http://hub.ironfroggy.com/
  29. http://www.twitter.com/ironfroggy
  30. mailto:TriZPUG at python.org
  31. https://mail.python.org/mailman/listinfo/trizpug
  32. http://tripython.org/


More information about the TriZPUG mailing list