Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #82293

Re: How to detect that a function argument is the default one

Path csiph.com!usenet.pasdenom.info!goblin2!goblin.stu.neva.ru!feeder1.cambriumusenet.nl!feed.tweaknews.nl!194.109.133.81.MISMATCH!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'attribute': 0.07; 'class,': 0.07; 'none:': 0.07; 'computed': 0.09; 'none)': 0.09; 'thx': 0.09; 'subject:How': 0.10; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; '@property': 0.16; 'attribute,': 0.16; 'expression.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'highlight': 0.16; "object's": 0.16; 'subject:default': 0.16; 'ternary': 0.16; 'unexpected': 0.16; 'wrote:': 0.18; 'thu,': 0.19; 'programming': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'question': 0.24; 'cc:2**0': 0.24; 'this:': 0.26; 'skip:_ 20': 0.27; 'header:In- Reply-To:1': 0.27; 'idea': 0.28; 'point': 0.28; 'am,': 0.29; 'dec': 0.30; 'message-id:@mail.gmail.com': 0.30; 'subject:that': 0.31; 'another': 0.32; '-----': 0.33; 'skip:_ 10': 0.34; 'maybe': 0.34; 'subject:the': 0.34; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'mass': 0.36; 'subject:one': 0.36; 'should': 0.36; 'changing': 0.37; 'being': 0.38; 'sometimes': 0.38; 'issue': 0.38; 'does': 0.39; 'aside': 0.39; 'circle': 0.68; 'us,': 0.73; 'obvious': 0.74; 'circles': 0.84; 'fortunately': 0.84; 'cast': 0.91; 'write:': 0.91; 'to:none': 0.92
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=zaQixbiaEGgQrCPAYAKsmYeXopnwO7tI3Ed4XuZLBjc=; b=PQ6XJhNL/v3RrM9MzZ+4AVSHkXO41o5x0yYanevJEAJnr0Yh4SRXtH1FaQojuKRGVw 50HYqqh8WL2S/CsGbfWhIvWxy1sUfRvKZ4ao+SPHzYBlw1ewwA42uyxKHQNqUGxXUXXb 7N9QaJBcFOQ0cO4sZHM5a1SkYOpJb1A4Z5NJ/dCmuu7b7sG2z9tMqSEdFFkN3n65DCCJ a7dl2nCHP+znqlPyGRaLkVU39/GTVTCkSa05BLeW21Ol4jVkKM2agWHkBfq4IXPmvB+p clf/SSzwFg88X/LS9SI6LPYQfYfAv58ohIWgY5rzgxfe4EV5LrBqP3w6Dtr21G4D7790 b+8A==
MIME-Version 1.0
X-Received by 10.50.61.140 with SMTP id p12mr8946072igr.34.1418228202984; Wed, 10 Dec 2014 08:16:42 -0800 (PST)
In-Reply-To <1967177562.5434671.1418227803051.JavaMail.root@sequans.com>
References <5488634e$0$2130$426a74cc@news.free.fr> <1967177562.5434671.1418227803051.JavaMail.root@sequans.com>
Date Thu, 11 Dec 2014 03:16:42 +1100
Subject Re: How to detect that a function argument is the default one
From Chris Angelico <rosuav@gmail.com>
Cc "python-list@python.org" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.16814.1418228205.18130.python-list@python.org> (permalink)
Lines 69
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1418228205 news.xs4all.nl 2882 [2001:888:2000:d::a6]:50826
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:82293

Show key headers only | View raw


On Thu, Dec 11, 2014 at 3:10 AM, Jean-Michel Pichavant
<jeanmichel@sequans.com> wrote:
> ----- Original Message -----
>> From: "ast" <nomail@invalid.com>
>> I have the idea to write:
>>
>> def __init__(center=(0,0), radius=10, mass=None)):
>>
>>     if  mass == None:
>>         self.mass = radius**2
>>     else:
>>         self.mass = mass
>>
>> but maybe Python provides something clever.
>>
>> Thx
>
> If you like one-liners,
>
> def __init__(self, center=(0,0), radius=10, mass=None):
>     self.center = center
>     self.radius = radius
>     self.mass = (mass is None and radius**2) or mass

Leaving aside the obvious point that not everything needs to be a
one-liner, there is a MUCH better way to spell this: the if/else
ternary expression.

self.mass = radius**2 if mass is None else mass

> But there's an issue with that solution: self.mass being computed during the instance initialization, what if radius is changing ?
>
> c1 = Circle((0,0), 10, None)
> print c1.mass
> 20
> c1.radius = 20
> print c1.mass
> 20 # that is unexpected
>
> Everytime an attribute is computed from another attribute, it should ring a python bell : property
>
> def __init__(self, center, radius, mass):
>     self.center = center
>     self.radius = radius
>     self._mass = mass
>
>     @property
>     def mass(self):
>          return self._mass if self._mass is not None else self.radius*2
>
>
> c1 = Circle((0,0), 10, None)
> print c1.mass
> 20
> c1.radius = 20
> print c1.mass
> 40

I think that juuuuuust miiiiight count as scope creep :) Though it
does highlight an interesting point, namely that sometimes the state
of "argument wasn't specified" needs to carry through into an object's
"attribute wasn't specified".

> Note : what is the mass of a circle ?

Fortunately for us, this is a programming class, not a philosophical
one. Maybe the circles in question are being cast in steel? :)

ChrisA

Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

How to detect that a function argument is the default one "ast" <nomail@invalid.com> - 2014-12-10 16:14 +0100
  Re: How to detect that a function argument is the default one Skip Montanaro <skip.montanaro@gmail.com> - 2014-12-10 09:29 -0600
    Re: How to detect that a function argument is the default one "ast" <nomail@invalid.com> - 2014-12-10 16:45 +0100
    Re: How to detect that a function argument is the default one Marko Rauhamaa <marko@pacujo.net> - 2014-12-10 18:33 +0200
  Re: How to detect that a function argument is the default one Chris Angelico <rosuav@gmail.com> - 2014-12-11 03:16 +1100
    Re: How to detect that a function argument is the default one wxjmfauth@gmail.com - 2014-12-10 12:08 -0800
    Re: How to detect that a function argument is the default one "ast" <nomail@invalid.com> - 2014-12-10 22:17 +0100
      Re: How to detect that a function argument is the default one Ian Kelly <ian.g.kelly@gmail.com> - 2014-12-11 11:03 -0700
      Re: How to detect that a function argument is the default one Ian Kelly <ian.g.kelly@gmail.com> - 2014-12-11 11:07 -0700
  Re: How to detect that a function argument is the default one Ned Batchelder <ned@nedbatchelder.com> - 2014-12-10 11:19 -0500
  Re: How to detect that a function argument is the default one Ian Kelly <ian.g.kelly@gmail.com> - 2014-12-10 09:20 -0700
  Re: How to detect that a function argument is the default one Tony the Tiger <tony@tiger.invalid> - 2014-12-11 19:45 +0000
    Re: How to detect that a function argument is the default one Ben Finney <ben+python@benfinney.id.au> - 2014-12-12 10:03 +1100

csiph-web