[TriPython] Let's talk about classes: invoking class methods with the class name
Jeff Heard
jefferson.r.heard at gmail.com
Thu Nov 3 21:57:40 EDT 2016
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 <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
> TriZPUG at python.org
> https://mail.python.org/mailman/listinfo/trizpug
> http://tripython.org is the Triangle Python Users Group
>
-------------- next part --------------
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]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]TriZPUG at python.org
[3]https://mail.python.org/mailman/listinfo/trizpug
[4]http://tripython.org is the Triangle Python Users Group
References
Visible links
1. mailto:lgtateos at ncsu.edu
2. mailto:TriZPUG at python.org
3. https://mail.python.org/mailman/listinfo/trizpug
4. http://tripython.org/
More information about the TriZPUG
mailing list