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


Groups > comp.lang.python > #82292 > unrolled thread

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

Started byJean-Michel Pichavant <jeanmichel@sequans.com>
First post2014-12-10 17:10 +0100
Last post2014-12-10 19:20 +0200
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  Re: How to detect that a function argument is the default one Jean-Michel Pichavant <jeanmichel@sequans.com> - 2014-12-10 17:10 +0100
    Re: How to detect that a function argument is the default one Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2014-12-10 19:20 +0200

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

FromJean-Michel Pichavant <jeanmichel@sequans.com>
Date2014-12-10 17:10 +0100
SubjectRe: How to detect that a function argument is the default one
Message-ID<mailman.16813.1418227811.18130.python-list@python.org>
----- 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

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

JM

Note : what is the mass of a circle ?


-- IMPORTANT NOTICE: 

The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.

[toc] | [next] | [standalone]


#82302

FromJussi Piitulainen <jpiitula@ling.helsinki.fi>
Date2014-12-10 19:20 +0200
Message-ID<qotmw6vxvzf.fsf@ruuvi.it.helsinki.fi>
In reply to#82292
Jean-Michel Pichavant writes:

> 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

That's not a one-liner. That's a one-liner:

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

And as others point out, you could highlight the number of
sub-expressions better by using an actual three-place expression.
(Why _that_ is considered so salient, I don't know. But it is.)

</deadpan>

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web