Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'guido': 0.05; 'true,': 0.05; 'url:pipermail': 0.05; 'c++,': 0.07; 'detect': 0.07; 'error:': 0.07; 'method.': 0.07; 'modified': 0.07; 'see.': 0.07; 'aliases': 0.09; 'check,': 0.09; 'false,': 0.09; 'false.': 0.09; 'function,': 0.09; 'methods,': 0.09; 'none)': 0.09; "people's": 0.09; 'referenced': 0.09; 'rossum': 0.09; 'wrapper': 0.09; 'python': 0.11; 'def': 0.12; "wouldn't": 0.14; '(false,': 0.16; '__new__': 0.16; 'bool': 0.16; 'boolean': 0.16; 'compares': 0.16; 'constructor.': 0.16; 'detected': 0.16; 'did,': 0.16; 'distinct': 0.16; 'existance': 0.16; 'expands': 0.16; 'expression.': 0.16; 'false:': 0.16; 'instance;': 0.16; 'instances,': 0.16; 'metaclass': 0.16; 'mylist': 0.16; 'operator.': 0.16; 'singleton': 0.16; 'subclass': 0.16; 'subclasses.': 0.16; 'tuple.': 0.16; 'typeerror:': 0.16; 'exception': 0.16; 'all.': 0.16; 'ignore': 0.16; 'language': 0.16; 'wrote:': 0.18; 'obviously': 0.18; 'unlike': 0.19; 'value.': 0.19; 'seems': 0.21; '>>>': 0.22; 'memory': 0.22; 'example': 0.22; 'separate': 0.22; 'tests': 0.22; 'print': 0.22; 'this?': 0.23; 'header:User-Agent:1': 0.23; 'error': 0.23; "aren't": 0.24; 'closely': 0.24; "shouldn't": 0.24; 'earlier': 0.24; 'java': 0.24; 'non': 0.24; 'class.': 0.26; 'compare': 0.26; 'post': 0.26; 'van': 0.27; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; 'idea': 0.28; 'am,': 0.29; 'expanding': 0.29; "doesn't": 0.30; 'andrew': 0.30; 'returned': 0.30; "i'm": 0.30; 'code': 0.31; '"",': 0.31; 'comparison': 0.31; 'object.': 0.31; 'ok.': 0.31; 'piece': 0.31; 'terms.': 0.31; 'file': 0.32; 'class': 0.32; 'url:python': 0.33; '(most': 0.33; 'checking': 0.33; 'totally': 0.33; 'skip:_ 10': 0.34; 'could': 0.34; "can't": 0.35; 'classes': 0.35; 'something': 0.35; 'no,': 0.35; 'objects': 0.35; 'but': 0.35; 'add': 0.35; 'there': 0.35; 'really': 0.36; 'acceptable': 0.36; 'false': 0.36; 'instances': 0.36; 'returning': 0.36; 'done': 0.36; 'doing': 0.36; 'shows': 0.36; 'useful': 0.36; 'possible': 0.36; 'url:org': 0.36; 'should': 0.36; 'searching': 0.37; 'so,': 0.37; 'too': 0.37; 'two': 0.37; 'level': 0.37; 'being': 0.38; 'checks': 0.38; 'whatever': 0.38; 'making': 0.63; 'name': 0.63; 'provide': 0.64; 'more': 0.64; 'yes': 0.68; 'night,': 0.68; 'realized': 0.68; 'received:74.208': 0.68; 'transparent': 0.68; 'default': 0.69; 'acts': 0.74; 'special': 0.74; 'valued': 0.78; '"it': 0.84; '08:30': 0.84; 'all!': 0.84; 'backdoor': 0.84; 'ever.': 0.84; 'everyone;': 0.84; 'frustrating': 0.84; "it'd": 0.84; 'saying:': 0.84; 'url:python-dev': 0.84 Date: Tue, 06 Jan 2015 09:02:01 -0500 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.4.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Comparisons and sorting of a numeric class.... References: <54ABB88A.7070504@r3dsolutions.com> <54ABC52A.1050507@davea.name> <54ABE383.3020801@r3dsolutions.com> In-Reply-To: <54ABE383.3020801@r3dsolutions.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:77dHYrGd9f/qB9T6a73jjNx13EsrQby+MkwHtJgwBOT XjKAQ/bcSJk/wmahvRw6cw+t/CF6TtyBvh4Wa2FEMhg7A27t5Z mcWWP6Lb0UfWHUOfBcbdoZFzihYT7oPdiLoyxfTYX9FVwm/xnh WgEBOKHIS08WrTZiltedSa8igNPhEu4N6Fa47YDeYP+7mcKkTI dbZHKpQEus7Yx7apIiesNkQj44ZKdjGTWM/q+FupHw+aL2nYFL hFslt+alo/Gw0CLAYaB1LD8JEfNjtsd9dp/SKfMORjXtthAdD0 rQUs3RpzhFTesvcsJWnHjCkWKuhPzzcXp18fNubPoLGnpQw7w0 EWbiKQrYkj6jvU8QpS0E= X-UI-Out-Filterresults: notjunk:1; X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 156 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1420552927 news.xs4all.nl 2964 [2001:888:2000:d::a6]:50463 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:83270 On 01/06/2015 08:30 AM, Andrew Robinson wrote: > >>> So, I'm not sure I can subclass boolean either because that too is a >>> built in class ... but I'm not sure how else to make an object that >>> acts as boolean False, but can be differentiated from false by the 'is' >>> operator. It's frustrating -- what good is subclassing, if one cant >>> subclass all the base classes the language has? I said earlier that I don't think it's possible to do what you're doing without your users code being somewhat aware of your changes. But as long as the user doesn't check for the subclass-ness of your bool-like function, you should manage. In Python, duck-typing is encouraged, unlike java or C++, where the only substitutable classes are subclasses. >> >> As I said above, make sure you have a constructor. If you still get >> an error, post a message that shows exactly what you did, and what >> exception you saw. >> > > OK. > I tried to subclass bool, using __new__ just to see if it would even > accept the definition... eg: python 2.7.5 > > >>> class UBool( bool ): > ... def __new__( self, default ): return bool.__new__( self, default ) > ... > Traceback (most recent call last): > File "", line 1, in > TypeError: Error when calling the metaclass bases > type 'bool' is not an acceptable base type > > I also tried using return int.__new__( self, bool(default) ) but that > too failed the exact same way. > > > I came across this in my searches, perhaps it has something to do with > why I can't do this? > https://mail.python.org/pipermail/python-dev/2002-March/020822.html > > I thought about this last night, and realized that you shouldn't be > allowed to subclass bool at all! A subclass would only be useful when > it has instances, but the mere existance of an instance of a subclass > of bool would break the invariant that True and False are the only > instances of bool! (An instance of a subclass of C is also an > instance of C.) I think it's important not to provide a backdoor to > create additional bool instances, so I think bool should not be > subclassable. > > ... > > --Guido van Rossum > > So, I think Guido may have done something so that there are only two > instances of bool, ever. > eg: False and True, which aren't truly singletons -- but follow the > singleton restrictive idea of making a single instance of an object do > the work for everyone; eg: of False being the only instance of bool > returning False, and True being the only instance of bool returning True. > > Why this is so important to Guido, I don't know ... but it's making it > VERY difficult to add named aliases of False which will still be > detected as False and type-checkable as a bool. If my objects don't > type check right -- they will likely break some people's legacy code... > and I really don't even care to create a new instance of the bool object > in memory which is what Guido seems worried about, rather I'm really > only after the ability to detect the subclass wrapper name as distinct > from bool False or bool True with the 'is' operator. If there were a There's already a contradiction in what you want. You say you don't want to create a new bool object (distinct from True and False), but you have to create an instance of your class. If it WERE a subclass of bool, it'd be a bool, and break singleton. If you ignore your subclass "requirement," 'is' should do the right thing. Whatever your class instance is, it won't be the same object as True or as False. > way to get the typecheck to match, That's a piece of legacy code which you won't be able to support, as far as I can see. > I wouldn't mind making a totally > separate class which returned the False instance; eg: something like an > example I modified from searching on the web: > > class UBool(): > def __nonzero__(self): return self.default > def __init__( self, default=False ): self.default = bool(default) > def default( self, default=False ): self.defualt = bool(default) > > but, saying: > >>> error=UBool(False) > >>> if error is False: print "type and value match" > ... > >>> > > Failed to have type and value match, and suggests that 'is' tests the > type before expanding the value. Not at all. It doesn't check the type or the value. It checks whether it's the SAME object. > It's rather non intuitive, and will break code -- for clearly error > expands to 'False' when evaluated without comparison functions like ==. > > >>> if not error: print "yes it is false" > ... > yes it is false No, the object False is not referenced in the above expression. You're checking the "falseness" of the expression. Same as if you say if not 0 if not mylist > >>> print error.__nonzero__() > False > >>> if error==False: print "It compares to False properly" > ... You control this one by your __eq__ method. > >>> > > So, both 'is' and == seems to compare the type before attempting to > expand the value. > As a simple cross check, I tried to make a one valued tuple. > > >>> a=(False,None) > >>> print a > (False, None) > >>> a=(False,) > >>> if a is False: print "yes!!!" > ... > >>> > >>> if not a: print "a is False" > ... > >>> if a == False: print "a is False" > > but that obviously failed, too; and if == fails to say False==False ... > well, it's just to sensitive for wrapper classes to be involved unless > they are a subclass of bool... > 1) read up more closely on special methods, and on the meanings of id() and 'is' 2) And don't expect that any change you make at this level could be transparent to all existing applications. It's a contradiction in terms. -- DaveA