Re: tutorial example

Discussion in 'Python' started by Ruben Charles, Nov 12, 2005.

  1. That is the diference between a method and a function.
    A method do something and a function return something.

    Example:

    def psum(n, m):
    print (n + m)

    def rsum(n, m):
    return (n +m)

    Then try this...


    >>> psum(2, 3)


    >>> a = psum(2, 3)


    >>> a


    >>> a = rsum(2, 3)


    >>> a


    You see it?
     
    Ruben Charles, Nov 12, 2005
    #1
    1. Advertising

  2. Ruben Charles wrote:
    > That is the diference between a method and a function.
    > A method do something and a function return something.
    >

    This is not quite correct. The difference between a method and a
    function is that the method is associated with a type or class object, a
    function is not.

    A method can be bound to a particular instance of an object or it can be
    unbound. In the latter case the first argument of the method call
    should be an instance of the type or class object. However, there is no
    builtin check to ensure that the first argument is in fact an instance.

    Please see the little example below.

    Colin W.
    # areaToy.py

    class Point(object):
    def __init__(self, x, y):
    self.x= x
    self.y= y

    class Area(object):
    def __init__(self, *pts):
    ''' pts is a list of points describing the enclosing polygon,
    in clockwise order, instances of Point. '''
    self.pts= pts[0]

    def size(self):
    nPts= len(self.pts)
    if nPts < 3:
    return 0
    elif nPts == 3:
    return 0.5 # replace this with triangle area calc
    else:
    pts= self.pts
    return Area(pts[0:2] + [pts[-1]]).size() + \
    Area(pts[1:]).size()

    pts= [Point(*pt) for pt in [(0, 1), (1, 1), (1, 0), (0, 0)]]
    area= Area(pts)
    size= area.size()
    print 'points:', pts
    print 'size:', size
    print 'Area.size:', Area.size
    print 'area.size:', area.size
    > Example:
    >
    > def psum(n, m):
    > print (n + m)
    >
    > def rsum(n, m):
    > return (n +m)
    >
    > Then try this...
    >
    >
    >
    >>>> psum(2, 3)

    >
    >
    >>>>a = psum(2, 3)

    >
    >
    >>>>a

    >
    >
    >>>>a = rsum(2, 3)

    >
    >
    >>>>a

    >
    >
    > You see it?
     
    Colin J. Williams, Nov 12, 2005
    #2
    1. Advertising

  3. Ruben Charles wrote:
    > That is the diference between a method and a function.
    > A method do something and a function return something.


    I'd rather say it's the difference between a procedure and a function,
    as it is used in e.g. Pascal. Procedures in Pascal don't return
    anything, more or less analogue to functions returning void in C/C++.

    --
    If I have been able to see further, it was only because I stood
    on the shoulders of giants. -- Isaac Newton

    Roel Schroeven
     
    Roel Schroeven, Nov 12, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nachiketh
    Replies:
    0
    Views:
    576
    Nachiketh
    Feb 23, 2004
  2. John Gabriele
    Replies:
    0
    Views:
    872
    John Gabriele
    Oct 30, 2003
  3. Rensjuh
    Replies:
    7
    Views:
    1,041
    Mabden
    Sep 2, 2004
  4. W. Watson
    Replies:
    13
    Views:
    1,117
    W. Watson
    Sep 20, 2007
  5. Linuxmank
    Replies:
    36
    Views:
    1,544
    Linuxmank
    Aug 8, 2009
Loading...

Share This Page