Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

Python Programming Language

Usage of the __and__ method


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

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

On May 30, 10:11 pm, theju <thejaswi.puthr@gmail.com> wrote:

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

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

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc