Welcome, guest | Sign In | My Account | Store | Cart

Boolean operations offers more than its simple ability to test for truth

Python, 3 lines
1
2
3
## example
unique = lambda x: reduce(lambda y,z: (not (z in y) and y.append(z) ) or y, x, [])
unique([3, 4, 5, 6, 5, 3, 7])

There are 2 properties in boolean operations that makes them useful: * Boolean operations offers lazy evaluation, and * Values that are not expecific false are considered true, and this could be used to inline a return statement (check "Truth Value Testing" in python doc) This properties may help to improbe the functional capabilities of python Also, this could be used for those smalltalk' programmers wich think that a return default value should be "self"

7 comments

Graham Fawcett 18 years, 7 months ago  # | flag

Faster unique function. This is not really related to your recipe, but rather to your unique-function. FYI, the fastest (most efficient) approach I'm aware of to return a list of unique values is to use a dictionary:

def unique(sequence):
    d = {}
    for item in sequence:
        d[item] = None
    return d.keys()
Javier Burroni (author) 18 years, 7 months ago  # | flag

Faster unique function. I was tempted to use the dict version wich is like:

unique = lambda x: reduce(lambda y,z: y.setdefault(z, None) or y, x, {}).keys()

unique([3, 4, 5, 6, 5, 3, 7])

I prefer no to do so because it doesn't keeps the sequence's order, but I agree it's faster (and notably faster on old python versions)

Ian Bicking 18 years, 7 months ago  # | flag

even shorter... Shorter:

def unique(seq):
    return dict.fromkeys(seq).keys()

Or even better:

def unique(seq):
    return sets.Set(seq)

Well, that's a little trivial, maybe just:

unique = sets.Set
Graham Fawcett 18 years, 7 months ago  # | flag

unique = sets.Set: +1. I'm not sure why that makes me want to smile, Ian; but it does. How succinct!

Since you still need a separate line to import 'sets', you could also use the one-liner:

from sets import Set as unique
Adrian Manrique 18 years, 7 months ago  # | flag

bollean operations, the kidss' friend. last night I was doing some stuff at my toilet... whatever! I was concerned about making code a little bit more maintable. so, given that I readed this recipe some time ago, inspiration came to me and :

class A:
    def method( self ):
        print 'foo'
        return 1

class B:
    def method(self):
        return len(B.__bases__) and B.__bases__[0].method(self) or self

this is super dooper, since I could get B as base class in the future without crashing anything. oh yeah! , thanks mr burroni for giving your knowledge to the people

amen/

Adrian Manrique 18 years, 7 months ago  # | flag

sorry there is a typo at class B definition

class A:
    def method( self ):
        print 'foo'
        return 1

class B(A):
    def method(self):
        return len(B.__bases__) and B.__bases__[0].method(self) or self
Javier Burroni (author) 18 years, 7 months ago  # | flag

you got the point ;)

Created by Javier Burroni on Tue, 30 Aug 2005 (PSF)
Python recipes (4591)
Javier Burroni's recipes (3)

Required Modules

  • (none specified)

Other Information and Tasks