Usage of the __and__ method

rishi pathak mailmaverick666 at gmail.com
Thu May 31 01:33:27 EDT 2007


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

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

#r = p and q #This does'nt call __and__
print p.__and__(q) #this works
#print r.print_name()


On 30 May 2007 22:11:45 -0700, theju <thejaswi.puthraya at gmail.com> wrote:
>
> 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
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
Regards--
Rishi Pathak
National PARAM Supercomputing Facility
Center for Development of Advanced Computing(C-DAC)
Pune University Campus,Ganesh Khind Road
Pune-Maharastra
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20070531/52c3802a/attachment.html>


More information about the Python-list mailing list