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

References <5488634e$0$2130$426a74cc@news.free.fr> <1967177562.5434671.1418227803051.JavaMail.root@sequans.com>
Date 2014-12-11 03:16 +1100
Subject Re: How to detect that a function argument is the default one
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.16814.1418228205.18130.python-list@python.org> (permalink)

Show all headers | 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