Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.014 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'python.': 0.02; 'python,': 0.02; 'suppose': 0.07; 'function,': 0.09; 'latter': 0.09; 'method,': 0.09; 'mapped': 0.16; 'subject:API': 0.16; 'subject:Object': 0.16; 'subject:oriented': 0.16; 'unbound': 0.16; 'java,': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'have:': 0.19; '>>>': 0.22; 'preferred': 0.22; 'python?': 0.22; 'instance,': 0.24; 'pass': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'message-id:@mail.gmail.com': 0.30; 'allows': 0.31; 'class': 0.32; 'implemented': 0.33; 'received:209.85': 0.35; 'received:209.85.220': 0.35; 'something': 0.35; 'objects': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'version': 0.36; 'choosing': 0.36; 'instances': 0.36; 'largely': 0.36; 'method': 0.36; 'useful': 0.36; 'two': 0.37; 'received:209': 0.37; 'implement': 0.38; 'e.g.': 0.38; 'version,': 0.38; 'to:addr :python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'skip:u 10': 0.60; 'matter': 0.61; 'personal': 0.63; 'such': 0.63; 'subject:. ': 0.67; 'between': 0.67; 'products': 0.71; 'yourself': 0.78; 'potentially': 0.81; 'wanting': 0.93; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:mime-version:in-reply-to:references:from:date:message-id :subject:to:content-type; bh=wBhkSD1zRIqygXbvQfsBMfD/oUonwOrREjH2dap3vig=; b=c+xbfKOtwjNDU4lrdAOBZIZ+d6lvb3a3O9fw2Ia6G9eIqC4cnAoijWw5QOQ44wuMln GobZP5rJiTh9mmat9PfsnFjix2sKTz+dHwa4RPXOl6SOx/QQNZvw/+8IAqHD2vln92JW KJxW0T8UEBBXBo6gtw32Lwdx+PW3sUvIZHu5blujFJIt9LK4mfKYxmYnhJaAR1SzqVaq DvPY0LizSUP8+L6XrHHbWKJW69AnyhWqOfYo/1TXxTk+1ikWkgR9SGtpGYj4/MfRPktk oJORaB37MW/Fy6JKyoubXVKC8Icyt3sogtn+w2o5R7jfAwsGrI3S7Eiv9EGDd48Pz05H hyWA== X-Received: by 10.66.250.230 with SMTP id zf6mr6394060pac.153.1365637288287; Wed, 10 Apr 2013 16:41:28 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: References: From: Ian Kelly Date: Wed, 10 Apr 2013 17:40:48 -0600 Subject: Re: Functional vs. Object oriented API To: Python Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 27 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1365637782 news.xs4all.nl 2687 [2001:888:2000:d::a6]:37357 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:43304 On Wed, Apr 10, 2013 at 5:16 PM, Max Bucknell wrote: > I also have a function to generate the dot product of these two vectors. In Java, such a function would be put as a method on the class and I would do something like: > > >>> a.dot_product(b) > 7 > > and that would be the end of it. But in Python, I can also have: > > >>> dot_product(a, b) > 7 > > Which of these two are preferred in Python? And are there any general guidelines for choosing between the two styles, or is it largely a matter of personal preference? The advantage to the latter is that it potentially allows you to implement dot products for other types using the same function. Using the method, a must be a Vector instance, but using the function it remains unrestricted. This is useful because functions are first-class objects in Python. Suppose that you find yourself wanting to pass that dot_product operation to some other function, e.g. map(). Using the function version you just pass in dot_product -- map(dot_product, seq1, seq2) -- and the mapped sequences can then contain any types that dot_product has been implemented to handle. Using the method version, you would have to pass in the unbound Vector.dot_product method -- map(Vector.dot_product, seq1, seq2), and then the method will only accept Vector instances in seq1.