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

Laura Tateosian lgtateos at ncsu.edu
Thu Nov 3 21:35:16 EDT 2016


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()
-------------- next part --------------
   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()


More information about the TriZPUG mailing list