473,320 Members | 1,870 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,320 software developers and data experts.

any way to customize the is operator?

There doesn't seem to be any way to customize the behavior of "is" as
can be done for other operators... why not?

Jan 27 '06 #1
9 1600
Lonnie Princehouse wrote:
There doesn't seem to be any way to customize the behavior of "is" as
can be done for other operators... why not?


because it does "id(a) == id(b)", and there's no way to customize
the behaviour of id().

(objects are not allowed to lie about who they are, or what they
are).

</F>

Jan 27 '06 #2
> (objects are not allowed to lie about who they are, or what they are).

Dangit! I need to find a less honest programming language. Anyone
have a Perl cookbook handy? ...

Jan 27 '06 #3
Lonnie Princehouse wrote:
(objects are not allowed to lie about who they are, or what they are).

Dangit! I need to find a less honest programming language. Anyone
have a Perl cookbook handy? ...


+1 QOTW (approved by a fellow Perl programmer FWIW !-)

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Jan 27 '06 #4
Lonnie Princehouse enlightened us with:
There doesn't seem to be any way to customize the behavior of "is" as
can be done for other operators... why not?


Pure logic: A == A or A != A. An object is another object or not.
Why would you want to change that?

Sybren
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa
Jan 27 '06 #5
On Thu, 26 Jan 2006 23:26:14 -0800, Lonnie Princehouse wrote:
(objects are not allowed to lie about who they are, or what they are).


Dangit! I need to find a less honest programming language. Anyone
have a Perl cookbook handy? ...


No, you need a better algorithm.
Why did you want to customize "is"?
--
Steven.

Jan 27 '06 #6
> Why did you want to customize "is"?

Well, mostly out of principle ;-)

But also because I'm wrapping a C library which passes around C structs
which are wrapped in shim C++ classes for a Boost.Python layer. Boost
Python does a marvelous job of translating between Python and C++ data
types; when a C structure is returned, it magically translates that
structure into a Python wrapper. The problem is that a _new_ Python
wrapper is created every time a pointer is returned, even if a wrapper
already exists for that particular pointer.

To illustrate: Suppose you have a function in C++ which is a simple
identity function, e.g.

template <typename T>
T *identity (T *x) {
return x;
}

Calling the wrapped version of this function from Python will produce a
Python wrapper which represents the same underlying C++ object, but is
not actually the same Python object:
b = identity(a)
b is a False

I wanted to override the behavior of "is" so that (a is b) would be
True --- which shouldn't have caused a problem, since the wrapper
class's attributes are read-only from Python. As it is, I've overriden
__cmp__ and __hash__, so at least I get the correct dictionary behavior
mydict = { a : 'something' }
b = identity(a)
b in mydict

True

It's quite possible that there is some way to do this correctly from
the Boost.Python side of things... my understanding of how to use
Boost.Python is minimal.

Feb 11 '06 #7
Lonnie Princehouse wrote:
Why did you want to customize "is"?

Well, mostly out of principle ;-)

But also because I'm wrapping a C library which passes around C structs
which are wrapped in shim C++ classes for a Boost.Python layer. Boost
Python does a marvelous job of translating between Python and C++ data
types; when a C structure is returned, it magically translates that
structure into a Python wrapper. The problem is that a _new_ Python
wrapper is created every time a pointer is returned, even if a wrapper
already exists for that particular pointer.

To illustrate: Suppose you have a function in C++ which is a simple
identity function, e.g.

template <typename T>
T *identity (T *x) {
return x;
}

Calling the wrapped version of this function from Python will produce a
Python wrapper which represents the same underlying C++ object, but is
not actually the same Python object:

b = identity(a)
b is a
False

I wanted to override the behavior of "is" so that (a is b) would be
True --- which shouldn't have caused a problem, since the wrapper
class's attributes are read-only from Python. As it is, I've overriden
__cmp__ and __hash__, so at least I get the correct dictionary behavior

mydict = { a : 'something' }
b = identity(a)
b in mydict


True

It's quite possible that there is some way to do this correctly from
the Boost.Python side of things... my understanding of how to use
Boost.Python is minimal.

I will adopt my usual stance here of unequivocally stating that there is
no way you can do what you want to. In the case of the "is" operator you
can only expect truth when the left- and right-hand side operands refer
to the same object.

This strategy is usually sound: typically, before the metaphorical ink
has dried on my post some upstart comes along to prove me completely
wrong. So, I have done about all I can to help you. It's up to the rest
of the community now to prove me wrong (as they have so many times in
the past :).

regards
Steve

PS: I'm afraid I couldn't resisit shortening yur odds somewhat by doing
what I usually fail to do and examining the source of is_() in
Modules/operator.c. I'm afraid it's not looking good ... the result of
"is" does appear to depend only on the memory addresses of the two
operands being the same:

result = (a1 == a2) ? Py_True : Py_False;
Py_INCREF(result);

sorry ...
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/

Feb 11 '06 #8
In article <11*********************@g47g2000cwa.googlegroups. com>,
Lonnie Princehouse <fi**************@gmail.com> wrote:
Why did you want to customize "is"?


Well, mostly out of principle ;-)

But also because I'm wrapping a C library which passes around C structs
which are wrapped in shim C++ classes for a Boost.Python layer. Boost
Python does a marvelous job of translating between Python and C++ data
types; when a C structure is returned, it magically translates that
structure into a Python wrapper. The problem is that a _new_ Python
wrapper is created every time a pointer is returned, even if a wrapper
already exists for that particular pointer.


Then you need to fix Boost.Python. This cannot and will not get fixed at
the Python level -- how can Python possibly know that they are the same
objects?
--
Aahz (aa**@pythoncraft.com) <*> http://www.pythoncraft.com/

"19. A language that doesn't affect the way you think about programming,
is not worth knowing." --Alan Perlis
Feb 11 '06 #9

"> Lonnie Princehouse wrote:
Calling the wrapped version of this function from Python will produce a
Python wrapper which represents the same underlying C++ object, but is
not actually the same Python object:


What I think you need is a custom version of the .__eq__ (self,other)
method that pierces thru the wrappers to compare the underlying C++
objects. Then test a == b.

In Python, 'is' specifically means identity of Python objects.


Feb 11 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Chris Kilmer | last post by:
I would like to be able to customize the path structure that VS.NET 2003 creates for projects. 1. I'd like to be able to create a project without VS.NET creating a folder for that project. ...
1
by: Shabam | last post by:
When I set validateRequest="true", and a user tries to submit html into the form, the server returns with a huge error page detailing what wasn't allowed. Is there a way to customize just this...
1
by: deko | last post by:
I've found that the "Remove Filter/Sort" selection in the Shortcut menu (displayed on right click) produces ugly, untrappable errors, even though "Allow Filters" is set to No in the subform. ...
14
by: deko | last post by:
The below code dials a phone number when the subform datasheet cell containing the number is double clicked. The problem is that the dialer application (c:\windows\dialer.exe) pops up windows on...
5
by: kanchy kang | last post by:
Hi,all as we know, we can override the operator of one object(for example __eq__). my question is, how to override the basic operator? for example, for any object comparison operator(including...
5
by: thinktwice | last post by:
key_compare can only return true, false , but how about equal happens?
5
by: DLN | last post by:
Getting rid of the Customize option. I want to get rid of the {Right-Click}-{Customize} option on the tool bars. I’ve been able to get rid of all the other options but not Customize itself. ...
0
Iman100
by: Iman100 | last post by:
Hi I have quiz (MCQ) data stored in my Database. I need to customize the question view to show the question then use RadioButtonList to display my choices. Is there is any tutorial or guide I can...
0
by: Sky | last post by:
I have an Access 2003 front-end database with custom toolbars. The toolbars work fine. One annoying feature is that at the far right edge of each custom toolbar there a small dropdown arrow....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.