Usage of the __and__ method

Peter Otten __peter__ at web.de
Thu May 31 01:45:26 EDT 2007


theju wrote:

> I've two objects (both instances of a class called Person) and I want
> to use the __and__ method and print the combined attributes of the two
> instances.

> r = p and q
> print r.print_name()

> The above output in both cases is giving me a doubt if __and__ method
> is over-ridable or not?

You cannot customize the logical 'and'. The __and__() method is used to
implement the binary and '&'. Change your code to

r = p & q
print r.print_name()

Peter



More information about the Python-list mailing list