Why does calling Python's 'magic method' not do type conversion like it would for the corresponding...
Do error bars on probabilities have any meaning?
Is it appropriate to give a culturally-traditional gift to a female coworker?
How to explain one side of Super Earth is smoother than the other side?
How should I ship cards?
Is the "hot network questions" element on Stack Overflow a dark pattern?
Would Refreshing a Sandbox Wipe Out Certain Metadata?
Tikz - highlight text in an image
How to announce in an ATIS message that two parallel runways are in use?
How can I portray body horror and still be sensitive to people with disabilities?
Identical projects by students at two different colleges: still plagiarism?
Smooth Density Histogram with probability areas
Is layered encryption more secure than long passwords?
Can a rabbi conduct a marriage if the bride is already pregnant from the groom?
How to make clear what a part-humanoid character looks like when they're quite common in their world?
Why did Tywin never remarry?
How can a kingdom keep the secret of a missing monarchy from the public?
Which was the first story to feature space elevators?
Why does Python copy numpy arrays where the length of the dimensions are the same?
Is it possible to detect 100% of SQLi with a simple regex?
Should corporate security training be tailored based on a users' job role?
Which was the first story to feature helmets which reads your mind to control a machine?
multiple price sets?
If an area is covered in both Ball Bearings and Caltrops, does the creature need to move at half speed or quarter speed to avoid both their effects?
Buying a "Used" Router
Why does calling Python's 'magic method' not do type conversion like it would for the corresponding operator?
Why are Python's 'private' methods not actually private?What happens in C++ when an integer type is cast to a floating point type or vice-versa?When does Python do automatic type conversion?Implicit conversion in C++Hibernate : java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Doubletype conversion operator not being used to cast, compilers differc++ type cast operator overloading and implicit conversionsWhy does the “as” operator not use an implicit conversion operator in C#?Implicit conversion while comparing numeric values in PythonWhy does static_cast not use the conversion operator to pointer to const?
When I subtract a float from an integer (e.g. 1-2.0), Python does implicit type conversion (I think). But when I call what I thought was the same operation using the magic method __sub__, it suddenly does not anymore.
What am I missing here? When I overload operators for my own classes, is there a way around this other than explicitly casting input to whatever type I need?
a=1
a.__sub__(2.)
# returns NotImplemented
a.__rsub__(2.)
# returns NotImplemented
# yet, of course:
a-2.
# returns -1.0
python casting
add a comment |
When I subtract a float from an integer (e.g. 1-2.0), Python does implicit type conversion (I think). But when I call what I thought was the same operation using the magic method __sub__, it suddenly does not anymore.
What am I missing here? When I overload operators for my own classes, is there a way around this other than explicitly casting input to whatever type I need?
a=1
a.__sub__(2.)
# returns NotImplemented
a.__rsub__(2.)
# returns NotImplemented
# yet, of course:
a-2.
# returns -1.0
python casting
add a comment |
When I subtract a float from an integer (e.g. 1-2.0), Python does implicit type conversion (I think). But when I call what I thought was the same operation using the magic method __sub__, it suddenly does not anymore.
What am I missing here? When I overload operators for my own classes, is there a way around this other than explicitly casting input to whatever type I need?
a=1
a.__sub__(2.)
# returns NotImplemented
a.__rsub__(2.)
# returns NotImplemented
# yet, of course:
a-2.
# returns -1.0
python casting
When I subtract a float from an integer (e.g. 1-2.0), Python does implicit type conversion (I think). But when I call what I thought was the same operation using the magic method __sub__, it suddenly does not anymore.
What am I missing here? When I overload operators for my own classes, is there a way around this other than explicitly casting input to whatever type I need?
a=1
a.__sub__(2.)
# returns NotImplemented
a.__rsub__(2.)
# returns NotImplemented
# yet, of course:
a-2.
# returns -1.0
python casting
python casting
asked 3 hours ago
dopplerdoppler
1316
1316
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
a - b isn't just a.__sub__(b). It also tries b.__rsub__(a) if a can't handle the operation, and in the 1 - 2. case, it's the float's __rsub__ that handles the operation.
>>> (2.).__rsub__(1)
-1.0
You ran a.__rsub__(2.), but that's the wrong __rsub__. You need the right-side operand's __rsub__, not the left-side operand.
There is no implicit type conversion built into the subtraction operator. float.__rsub__ has to handle ints manually. If you want type conversion in your own operator implementations, you'll have to handle that manually too.
5
It's worth noting that theNotImplementedresult that is returned by the calls in the question are the signal to try the reverse method.
– Blckknght
2 hours ago
Thanks! I was aware it would try__rsub__but didn't know it would reverse the argument order.
– doppler
2 hours ago
1
@doppler: It'd be pretty pointless to have the left operand handle both__sub__and__rsub__. That'd just be two methods with the exact same job, and the right operand would have no opportunity to supply an implementation.
– user2357112
2 hours ago
@user2357112 soself.__rsub__(other)really just callsother.__sub__(self), if that makes any sense?
– doppler
2 hours ago
2
@doppler: No.self.__rsub__(other)is called forother - selfifothercan't handle it. Callingother.__sub__(self)would be pointless. We already knowothercan't handle it.
– user2357112
2 hours ago
|
show 1 more comment
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54775946%2fwhy-does-calling-pythons-magic-method-not-do-type-conversion-like-it-would-fo%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
a - b isn't just a.__sub__(b). It also tries b.__rsub__(a) if a can't handle the operation, and in the 1 - 2. case, it's the float's __rsub__ that handles the operation.
>>> (2.).__rsub__(1)
-1.0
You ran a.__rsub__(2.), but that's the wrong __rsub__. You need the right-side operand's __rsub__, not the left-side operand.
There is no implicit type conversion built into the subtraction operator. float.__rsub__ has to handle ints manually. If you want type conversion in your own operator implementations, you'll have to handle that manually too.
5
It's worth noting that theNotImplementedresult that is returned by the calls in the question are the signal to try the reverse method.
– Blckknght
2 hours ago
Thanks! I was aware it would try__rsub__but didn't know it would reverse the argument order.
– doppler
2 hours ago
1
@doppler: It'd be pretty pointless to have the left operand handle both__sub__and__rsub__. That'd just be two methods with the exact same job, and the right operand would have no opportunity to supply an implementation.
– user2357112
2 hours ago
@user2357112 soself.__rsub__(other)really just callsother.__sub__(self), if that makes any sense?
– doppler
2 hours ago
2
@doppler: No.self.__rsub__(other)is called forother - selfifothercan't handle it. Callingother.__sub__(self)would be pointless. We already knowothercan't handle it.
– user2357112
2 hours ago
|
show 1 more comment
a - b isn't just a.__sub__(b). It also tries b.__rsub__(a) if a can't handle the operation, and in the 1 - 2. case, it's the float's __rsub__ that handles the operation.
>>> (2.).__rsub__(1)
-1.0
You ran a.__rsub__(2.), but that's the wrong __rsub__. You need the right-side operand's __rsub__, not the left-side operand.
There is no implicit type conversion built into the subtraction operator. float.__rsub__ has to handle ints manually. If you want type conversion in your own operator implementations, you'll have to handle that manually too.
5
It's worth noting that theNotImplementedresult that is returned by the calls in the question are the signal to try the reverse method.
– Blckknght
2 hours ago
Thanks! I was aware it would try__rsub__but didn't know it would reverse the argument order.
– doppler
2 hours ago
1
@doppler: It'd be pretty pointless to have the left operand handle both__sub__and__rsub__. That'd just be two methods with the exact same job, and the right operand would have no opportunity to supply an implementation.
– user2357112
2 hours ago
@user2357112 soself.__rsub__(other)really just callsother.__sub__(self), if that makes any sense?
– doppler
2 hours ago
2
@doppler: No.self.__rsub__(other)is called forother - selfifothercan't handle it. Callingother.__sub__(self)would be pointless. We already knowothercan't handle it.
– user2357112
2 hours ago
|
show 1 more comment
a - b isn't just a.__sub__(b). It also tries b.__rsub__(a) if a can't handle the operation, and in the 1 - 2. case, it's the float's __rsub__ that handles the operation.
>>> (2.).__rsub__(1)
-1.0
You ran a.__rsub__(2.), but that's the wrong __rsub__. You need the right-side operand's __rsub__, not the left-side operand.
There is no implicit type conversion built into the subtraction operator. float.__rsub__ has to handle ints manually. If you want type conversion in your own operator implementations, you'll have to handle that manually too.
a - b isn't just a.__sub__(b). It also tries b.__rsub__(a) if a can't handle the operation, and in the 1 - 2. case, it's the float's __rsub__ that handles the operation.
>>> (2.).__rsub__(1)
-1.0
You ran a.__rsub__(2.), but that's the wrong __rsub__. You need the right-side operand's __rsub__, not the left-side operand.
There is no implicit type conversion built into the subtraction operator. float.__rsub__ has to handle ints manually. If you want type conversion in your own operator implementations, you'll have to handle that manually too.
edited 2 hours ago
answered 2 hours ago
user2357112user2357112
155k12163258
155k12163258
5
It's worth noting that theNotImplementedresult that is returned by the calls in the question are the signal to try the reverse method.
– Blckknght
2 hours ago
Thanks! I was aware it would try__rsub__but didn't know it would reverse the argument order.
– doppler
2 hours ago
1
@doppler: It'd be pretty pointless to have the left operand handle both__sub__and__rsub__. That'd just be two methods with the exact same job, and the right operand would have no opportunity to supply an implementation.
– user2357112
2 hours ago
@user2357112 soself.__rsub__(other)really just callsother.__sub__(self), if that makes any sense?
– doppler
2 hours ago
2
@doppler: No.self.__rsub__(other)is called forother - selfifothercan't handle it. Callingother.__sub__(self)would be pointless. We already knowothercan't handle it.
– user2357112
2 hours ago
|
show 1 more comment
5
It's worth noting that theNotImplementedresult that is returned by the calls in the question are the signal to try the reverse method.
– Blckknght
2 hours ago
Thanks! I was aware it would try__rsub__but didn't know it would reverse the argument order.
– doppler
2 hours ago
1
@doppler: It'd be pretty pointless to have the left operand handle both__sub__and__rsub__. That'd just be two methods with the exact same job, and the right operand would have no opportunity to supply an implementation.
– user2357112
2 hours ago
@user2357112 soself.__rsub__(other)really just callsother.__sub__(self), if that makes any sense?
– doppler
2 hours ago
2
@doppler: No.self.__rsub__(other)is called forother - selfifothercan't handle it. Callingother.__sub__(self)would be pointless. We already knowothercan't handle it.
– user2357112
2 hours ago
5
5
It's worth noting that the
NotImplemented result that is returned by the calls in the question are the signal to try the reverse method.– Blckknght
2 hours ago
It's worth noting that the
NotImplemented result that is returned by the calls in the question are the signal to try the reverse method.– Blckknght
2 hours ago
Thanks! I was aware it would try
__rsub__ but didn't know it would reverse the argument order.– doppler
2 hours ago
Thanks! I was aware it would try
__rsub__ but didn't know it would reverse the argument order.– doppler
2 hours ago
1
1
@doppler: It'd be pretty pointless to have the left operand handle both
__sub__ and __rsub__. That'd just be two methods with the exact same job, and the right operand would have no opportunity to supply an implementation.– user2357112
2 hours ago
@doppler: It'd be pretty pointless to have the left operand handle both
__sub__ and __rsub__. That'd just be two methods with the exact same job, and the right operand would have no opportunity to supply an implementation.– user2357112
2 hours ago
@user2357112 so
self.__rsub__(other) really just calls other.__sub__(self), if that makes any sense?– doppler
2 hours ago
@user2357112 so
self.__rsub__(other) really just calls other.__sub__(self), if that makes any sense?– doppler
2 hours ago
2
2
@doppler: No.
self.__rsub__(other) is called for other - self if other can't handle it. Calling other.__sub__(self) would be pointless. We already know other can't handle it.– user2357112
2 hours ago
@doppler: No.
self.__rsub__(other) is called for other - self if other can't handle it. Calling other.__sub__(self) would be pointless. We already know other can't handle it.– user2357112
2 hours ago
|
show 1 more comment
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54775946%2fwhy-does-calling-pythons-magic-method-not-do-type-conversion-like-it-would-fo%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown