473,396 Members | 2,024 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

May 31 '07 #1
3 1426
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: tomvr | last post by:
Hello I have noticed some 'weird' memory usage in a vb.net windows app The situation is as follows I have an app (heavy on images) with 2 forms (actually there are more forms and on starting...
2
by: Jarvis | last post by:
I've made a testing program to test the memory usage of some Data Forms. I create a MDI parent form with one single MDI child form, which is a Data Form generated by .NET Data Form Wizard. To...
3
by: Ian Taite | last post by:
Hello, I'm exploring why one of my C# .NET apps has "high" memory usage, and whether I can reduce the memory usage. I have an app that wakes up and processes text files into a database...
7
by: George Gre | last post by:
Hi, I wrote a c# programme that listens to incoming TCP requests and services them. This programme is meant to be running as long as the server its installed on is running. So we assume for...
3
by: Minh Khoa | last post by:
Please give me more information about delegate and its usage? Why do i use it and when?
7
by: LBT | last post by:
I have a window service written using VB.NET. This window service will scan folders for file and grab the file content to be inserted to SQL Server on file detection. There are altogether 18...
1
by: Tim T. | last post by:
I'm currently working on a report to forecast production for finished goods. The user can select one or more items to forecast. In addition, they may select one or more warehouses to view...
2
by: Fish | last post by:
I have been researching the correct way to organize my solution so that it makes best use of VB.NET inherent ability to manage resources such as objects. My solution contains 2 projects and the...
2
by: =?Utf-8?B?dXJrZWM=?= | last post by:
I am trying to create an in-process WMI provider using System.Management.Instrumentation namespace. For testing I use a simple class as a wrapper for FileInfo class. I have been able to use all...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.