Created
May 18, 2011 13:17
-
-
Save alvesjnr/978551 to your computer and use it in GitHub Desktop.
List wich accept & operator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyList(list): | |
def __and__(self, other): | |
op = str(bin(other)[2:]) | |
op = op.zfill(len(self))[::-1] | |
acc = [] | |
for count,i in enumerate(op): | |
if int(i): | |
acc.append(self[count]) | |
return acc | |
def __rand__(self,other): | |
return self.__and__(other) | |
if __name__=='__main__': | |
''' | |
A simple example that uses & operator for combining elements of a list | |
''' | |
l = MyList([1,2,3,4,5]) | |
for i in range(2**len(l)): | |
print l & i |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment