By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,676 Members | 2,249 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,676 IT Pros & Developers. It's quick & easy.

Usage of the __and__ method

P: n/a
Hello all,
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.

To be precise, here is my code....

class Person:
def __init__(self,name):
self.name = name
def print_name(self):
print self.name
def __and__(self,other):
self.name = '%s AND %s' %(self.name,other.name)
return self.name

p = Person("John")
q = Person("George")

r = p and q
print r.print_name()

Output:
-----------
George
None

I've also tried this:
class Person:
def __init__(self,name):
self.name = name
def print_name(self):
print self.name
def __and__(self,other):
a = Person()
a.name = '%s AND %s' %(self.name,other.name)
return a

p = Person("John")
q = Person("George")

r = p and q
print r.print_name()

Output:
-----------
George
None

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

The output that I am accepting is:
John AND George

What are the changes if to be made?

Thanking You
Thejaswi Puthraya
http://thejuhyd.blogspot.com

May 31 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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
May 31 '07 #2

P: n/a
On May 30, 10:11 pm, theju <thejaswi.puthr...@gmail.comwrote:
Hello all,
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.

To be precise, here is my code....

class Person:
def __init__(self,name):
self.name = name
def print_name(self):
print self.name
def __and__(self,other):
self.name = '%s AND %s' %(self.name,other.name)
return self.name

p = Person("John")
q = Person("George")

r = p and q
print r.print_name()

Try:

class Person(object):
def __init__(self, name):
self.name = name
def __and__(self, other):
return '%s AND %s' % (self.name, other.name)

p = Person("John")
q = Person("George")

r = p & q
print r
(1) A "getter" method (like your print_name
method) is usually not needed, just access the
attribute of the instance. Like,

print p.name

(2) I doubt that you want the __and__ special
method to alter the name attribute of an
instance.

(3) You want to use the '&' operator
to dispatch to the __and__ method; __and__
is typically used for Numeric objects.

(4) You misunderstood how the 'and' operator
is used. The expression 'p and q' causes p to
be evaluated; if p is false, its value is returned;
otherwise q is evaluated and its value is returned.

--
Hope this helps,
Steven

May 31 '07 #3

P: n/a
Thank you folks for reminding me that the logical AND cannot be over-
ridden and that the __and__ method represents the bit-wise AND
operation.

Thank You
Thejaswi Puthraya
http://thejuhyd.blogspot.com

May 31 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.