Path: csiph.com!usenet.pasdenom.info!nntpfeed.proxad.net!proxad.net!feeder1-1.proxad.net!217.73.144.45.MISMATCH!feeder2.ecngs.de!ecngs!feeder.ecngs.de!border1.nntp.ams1.giganews.com!nntp.giganews.com!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'argument': 0.05; '"""': 0.07; 'explicit': 0.07; 'modify': 0.07; 'e.g.,': 0.09; 'function,': 0.09; 'jesus': 0.09; 'meaningful': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; 'def': 0.12; '(x,': 0.16; '*always*': 0.16; 'argument,': 0.16; 'collections': 0.16; 'finney': 0.16; 'mutable': 0.16; 'parameter.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'self.y': 0.16; 'passing': 0.19; 'import': 0.22; 'header:User-Agent:1': 0.23; 'aspect': 0.24; 'driven': 0.24; 'space.': 0.24; 'together.': 0.24; 'define': 0.26; 'pass': 0.26; 'defined': 0.27; 'values': 0.27; 'header:X -Complaints-To:1': 0.27; 'point': 0.28; 'specified': 0.30; 'question:': 0.31; 'religious': 0.31; 'writes:': 0.31; 'class': 0.32; 'skip:c 30': 0.32; 'another': 0.32; 'url:python': 0.33; 'skip:_ 10': 0.34; 'skip:d 20': 0.34; 'but': 0.35; 'object,': 0.36; 'useful': 0.36; "i'll": 0.36; 'subject:?': 0.36; 'url:org': 0.36; 'should': 0.36; 'too': 0.37; 'list': 0.37; 'list.': 0.37; 'ben': 0.38; 'url:library': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'most': 0.60; 'deliver': 0.61; 'url:3': 0.61; "you're": 0.61; "you've": 0.63; 'address': 0.63; 'skip:\xe2 10': 0.65; 'default': 0.69; 'special': 0.74; 'subject:this': 0.83; 'flower': 0.84; 'paradox': 0.84; 'received:125': 0.84; 'terrible': 0.84; 'together,': 0.84; 'sensibly': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Ben Finney Subject: Re: Is this unpythonic? Date: Fri, 08 May 2015 21:31:44 +1000 References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Gmane-NNTP-Posting-Host: jigong.madmonks.org X-Public-Key-ID: 0xAC128405 X-Public-Key-Fingerprint: 517C F14B B2F3 98B0 CB35 4855 B8B2 4C06 AC12 8405 X-Public-Key-URL: http://www.benfinney.id.au/contact/bfinney-pubkey.asc X-Post-From: Ben Finney User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux) Cancel-Lock: sha1:2NcJibP9QcKmNLjaQ0eUKIpUfuQ= X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ 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: 69 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1431084717 news.xs4all.nl 2894 [2001:888:2000:d::a6]:58594 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:90147 "Frank Millman" writes: > If I use a list as an argument, I only use it to pass values *into* > the function, but I never modify the list. You've had good reasons why a mutable default argument is a good idea. I'll address another aspect of the question: passing a structured collection of values via a single parameter. If those values *always* go together, it sounds like you have a class which should be defined to make it explicit that they go together. E.g., a contrived example:: def deliver_flowers(species, location=[0, 0, 0]): """ Deliver the species of flower to the specified location. """ (x, y, z) = location vehicle.load_with(species) vehicle.drive_to(x, y, z) vehicle.unload() If the only reason you're using a collection is because those values sensibly go together as a single named object, then define a class for that:: class Point: """ A three-dimensional point in space. """ def __init__(self, x, y, z): self.x = x self.y = y self.z = z centre = Point(0, 0, 0) def deliver_flowers(species, location=centre): """ Deliver the species of flower to the specified location. """ vehicle.load_with(species) vehicle.drive_to(location.x, location.y, location.z) vehicle.unload() Too much overhead? If you want to collect a meaningful collection of values together in the same way each time, but don't need any special behaviour, then a class might be overkill. Python has the ‘namedtuple’ type:: import collections Point = collections.namedtuple('Point', ['x', 'y', 'z']) centre = Point(0, 0, 0) def deliver_flowers(species, location=centre): """ Deliver the species of flower to the specified location. """ vehicle.load_with(species) vehicle.drive_to(location.x, location.y, location.z) vehicle.unload() documents the surprisingly useful ‘namedtuple’ factory. -- \ “It's a terrible paradox that most charities are driven by | `\ religious belief.… if you think altruism without Jesus is not | _o__) altruism, then you're a dick.” —Tim Minchin, 2010-11-28 | Ben Finney