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


Groups > comp.lang.python > #57147

Detecting whether a value was passed for a parameter (was: skipping __init__ and using exploiting a class member instead)

Path csiph.com!usenet.pasdenom.info!news.albasani.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'value,': 0.04; '"""': 0.07; 'context': 0.07; '8bit%:30': 0.09; '__init__': 0.09; 'attributes': 0.09; 'caller': 0.09; 'instance.': 0.09; 'naturally': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:using': 0.09; 'python': 0.11; 'def': 0.12; 'accident.': 0.16; 'cleanly': 0.16; 'finney': 0.16; 'foo(object):': 0.16; 'magic': 0.16; 'object()': 0.16; 'parameter.': 0.16; 'popping': 0.16; 'rather,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'sentinel': 0.16; 'singleton': 0.16; 'subject:Detecting': 0.16; 'subject:class': 0.16; 'subject:member': 0.16; 'thought.': 0.16; 'utterly': 0.16; 'passing': 0.19; 'value.': 0.19; 'header:User-Agent:1': 0.23; 'class.': 0.26; 'right.': 0.26; 'header:X-Complaints-To:1': 0.27; 'point': 0.28; 'specifically': 0.29; 'thus': 0.29; 'originally': 0.30; 'url:wiki': 0.31; 'context,': 0.31; 'lies': 0.31; 'piece': 0.31; 'url:wikipedia': 0.31; 'values.': 0.31; 'writes:': 0.31; 'class': 0.32; 'agreed': 0.32; 'handled': 0.32; 'cases': 0.33; 'skip:_ 10': 0.34; 'subject: (': 0.35; "can't": 0.35; 'possible.': 0.35; 'but': 0.35; 'add': 0.35; 'there': 0.35; 'really': 0.36; 'method': 0.36; 'possible': 0.36; 'url:org': 0.36; 'should': 0.36; 'ben': 0.38; 'to:addr:python-list': 0.38; 'skip:_ 30': 0.39; 'expect': 0.39; 'though,': 0.39; 'to:addr:python.org': 0.39; 'enough': 0.39; 'received:org': 0.40; 'called': 0.40; 'new': 0.61; "you're": 0.61; 'skip:n 10': 0.64; 'more': 0.64; 'occur': 0.65; 'skip:\xe2 10': 0.65; 'here': 0.66; '8bit%:40': 0.68; 'default': 0.69; '8bit%:43': 0.74; 'truth': 0.81; '"not': 0.84; 'everywhere.': 0.84; 'flag.': 0.84; 'generations,': 0.84; 'isolated': 0.84; 'localized': 0.84; 'protocol,': 0.84; 'lazy': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Ben Finney <ben+python@benfinney.id.au>
Subject Detecting whether a value was passed for a parameter (was: skipping __init__ and using exploiting a class member instead)
Date Sun, 20 Oct 2013 22:50:57 +1100
References <fb1b5b6f-619c-4acd-b1bf-a375ccc6f1a6@googlegroups.com> <f33e6804-fa2f-4301-9bcd-222faf643ac9@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8
Content-Transfer-Encoding 8bit
X-Gmane-NNTP-Posting-Host rasputin.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-gpg.asc
X-Post-From Ben Finney <bignose+hates-spam@benfinney.id.au>
User-Agent Gnus/5.13 (Gnus v5.13) Emacs/23.4 (gnu/linux)
Cancel-Lock sha1:0dVbw8iaRxlS093K2BBwZBBDuTk=
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.1282.1382269876.18130.python-list@python.org> (permalink)
Lines 59
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1382269876 news.xs4all.nl 15968 [2001:888:2000:d::a6]:38768
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:57147

Show key headers only | View raw


Peter Cacioppi <peter.cacioppi@gmail.com> writes:

> I was laboring under some misconception that there was Python magic
> that allowed __init__ and only __init__ to add class attributes by
> setting their values. Good to know this piece of magic isn't part of
> Python, and thus lazy eval can be handled more cleanly than I
> originally thought.

Rather, the magic is that ‘__init__’ will be called automatically by the
class creation protocol, to initialise any new instance of that class.

So the method to write if you want instance-specific initialisation is
still ‘__init__’.

> Think about it this way. None here really means "not yet initialized".
> It is a value that cannot occur naturally and thus functions as a
> not-initialized flag.

As you point out, though, that's not a menaing of None agreed on
everywhere. You can't expect that the caller will not be passing a value
of None for that parameter.

> So this, somewhat arbitrary, context sensitive value should be isolated
> as much as possible. You don't want it popping up hither and yon, you
> want to type as infrequently as possible and localized it to as few
> methods as possible.

Right. The pattern you're looking for is called a “sentinel value”
<URL:https://en.wikipedia.org/wiki/Sentinel_value>.

Usually, the None singleton is good enough for a sentinel value, but
often it has a meaning already in your context, so is not available as a
sentinel value.

In cases where None is not available, make a specific value that has no
other meaning, and use that specifically for your parameter's sentinel
value::

    class Foo(object):
        _default_for_x = object()

        def __init__(self, x=_default_for_object):
            """ Initialise a new instance. """
            if x is self._default_for_object:
                # No value for ‘x’ was passed, because ‘_default_for_x’
                # would not be passed by accident.
                x = self._get_value_for_x()
            self.x = x

        def _get_default_value_for_x(self):
            """" Calculate the default ‘x’ value for this instance. """
            return some_computation(self)

-- 
 \       “When a well-packaged web of lies has been sold to the masses |
  `\    over generations, the truth will seem utterly preposterous and |
_o__)                    its speaker a raving lunatic.” —Dresden James |
Ben Finney

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


Thread

skipping __init__ and using exploiting a class member instead Peter Cacioppi <peter.cacioppi@gmail.com> - 2013-10-19 14:44 -0700
  Re: skipping __init__ and using exploiting a class member instead Ned Batchelder <ned@nedbatchelder.com> - 2013-10-19 17:55 -0400
  Re: skipping __init__ and using exploiting a class member instead Peter Cacioppi <peter.cacioppi@gmail.com> - 2013-10-19 15:11 -0700
  Re: skipping __init__ and using exploiting a class member instead Robert Kern <robert.kern@gmail.com> - 2013-10-19 23:18 +0100
  Re: skipping __init__ and using exploiting a class member instead Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-10-19 22:30 +0000
  Re: skipping __init__ and using exploiting a class member instead Peter Cacioppi <peter.cacioppi@gmail.com> - 2013-10-19 17:07 -0700
  Re: skipping __init__ and using exploiting a class member instead Peter Cacioppi <peter.cacioppi@gmail.com> - 2013-10-19 17:42 -0700
    Re: skipping __init__ and using exploiting a class member instead Ned Batchelder <ned@nedbatchelder.com> - 2013-10-19 21:13 -0400
    Detecting whether a value was passed for a parameter (was: skipping __init__ and using exploiting a class member instead) Ben Finney <ben+python@benfinney.id.au> - 2013-10-20 22:50 +1100
  Re: skipping __init__ and using exploiting a class member instead Peter Cacioppi <peter.cacioppi@gmail.com> - 2013-10-20 00:09 -0700
    Re: skipping __init__ and using exploiting a class member instead Roy Smith <roy@panix.com> - 2013-10-20 08:55 -0400
    Re: skipping __init__ and using exploiting a class member instead Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-10-20 14:16 +0100
  Re: skipping __init__ and using exploiting a class member instead Peter Cacioppi <peter.cacioppi@gmail.com> - 2013-10-20 01:46 -0700
  Re: skipping __init__ and using exploiting a class member instead Devin Jeanpierre <jeanpierreda@gmail.com> - 2013-10-20 07:27 -0700
  Re: skipping __init__ and using exploiting a class member instead Peter Cacioppi <peter.cacioppi@gmail.com> - 2013-10-20 10:57 -0700
    Re: skipping __init__ and using exploiting a class member instead Roy Smith <roy@panix.com> - 2013-10-20 16:44 -0400
    Re: skipping __init__ and using exploiting a class member instead Chris Angelico <rosuav@gmail.com> - 2013-10-21 07:57 +1100
      Re: skipping __init__ and using exploiting a class member instead Peter Cacioppi <peter.cacioppi@gmail.com> - 2013-10-20 14:34 -0700
        Re: skipping __init__ and using exploiting a class member instead Roy Smith <roy@panix.com> - 2013-10-20 18:31 -0400
          Re: skipping __init__ and using exploiting a class member instead Ben Finney <ben+python@benfinney.id.au> - 2013-10-21 10:55 +1100
            Re: skipping __init__ and using exploiting a class member instead Neil Cerutti <neilc@norwich.edu> - 2013-10-21 18:47 +0000
  Re: skipping __init__ and using exploiting a class member instead Peter Cacioppi <peter.cacioppi@gmail.com> - 2013-10-20 21:30 -0700
  Re: skipping __init__ and using exploiting a class member instead Peter Cacioppi <peter.cacioppi@gmail.com> - 2013-10-20 21:31 -0700
    Re: skipping __init__ and using exploiting a class member instead feedthetroll@gmx.de - 2013-10-21 00:16 -0700

csiph-web