How to do operator overloading between two different classes in python
Consider the main class
class point2D:
def __init__(x, y):
self.x = x
self.y = y
def __sub__(self, other):
return vector2D(self.x - other.x, self.y - other.y)
and the subclass:
class vector2D(point2D):
def __add__(self, other):
return vector2D(self.x + other.x, self.y + other.y)
now I want the +
operator also to be able to add a vector2D
and a point2D
object and return a point2D
object. And -
operator between any of the classes to return vector2D
anyway. I would appreciate if you could help me know if/how can I do that in Python 3.*
.
python class operator-overloading
add a comment |
Consider the main class
class point2D:
def __init__(x, y):
self.x = x
self.y = y
def __sub__(self, other):
return vector2D(self.x - other.x, self.y - other.y)
and the subclass:
class vector2D(point2D):
def __add__(self, other):
return vector2D(self.x + other.x, self.y + other.y)
now I want the +
operator also to be able to add a vector2D
and a point2D
object and return a point2D
object. And -
operator between any of the classes to return vector2D
anyway. I would appreciate if you could help me know if/how can I do that in Python 3.*
.
python class operator-overloading
1
Why doesvector2D
subclasspoint2D
? A vector is not a point.
– user2357112
Nov 14 '18 at 23:15
well, mathematically speaking true, but computationally: it can inherit all the methods without any issues.
– Foad
Nov 14 '18 at 23:17
add a comment |
Consider the main class
class point2D:
def __init__(x, y):
self.x = x
self.y = y
def __sub__(self, other):
return vector2D(self.x - other.x, self.y - other.y)
and the subclass:
class vector2D(point2D):
def __add__(self, other):
return vector2D(self.x + other.x, self.y + other.y)
now I want the +
operator also to be able to add a vector2D
and a point2D
object and return a point2D
object. And -
operator between any of the classes to return vector2D
anyway. I would appreciate if you could help me know if/how can I do that in Python 3.*
.
python class operator-overloading
Consider the main class
class point2D:
def __init__(x, y):
self.x = x
self.y = y
def __sub__(self, other):
return vector2D(self.x - other.x, self.y - other.y)
and the subclass:
class vector2D(point2D):
def __add__(self, other):
return vector2D(self.x + other.x, self.y + other.y)
now I want the +
operator also to be able to add a vector2D
and a point2D
object and return a point2D
object. And -
operator between any of the classes to return vector2D
anyway. I would appreciate if you could help me know if/how can I do that in Python 3.*
.
python class operator-overloading
python class operator-overloading
edited Nov 14 '18 at 23:17
Foad
asked Nov 14 '18 at 23:13
FoadFoad
1,70321334
1,70321334
1
Why doesvector2D
subclasspoint2D
? A vector is not a point.
– user2357112
Nov 14 '18 at 23:15
well, mathematically speaking true, but computationally: it can inherit all the methods without any issues.
– Foad
Nov 14 '18 at 23:17
add a comment |
1
Why doesvector2D
subclasspoint2D
? A vector is not a point.
– user2357112
Nov 14 '18 at 23:15
well, mathematically speaking true, but computationally: it can inherit all the methods without any issues.
– Foad
Nov 14 '18 at 23:17
1
1
Why does
vector2D
subclass point2D
? A vector is not a point.– user2357112
Nov 14 '18 at 23:15
Why does
vector2D
subclass point2D
? A vector is not a point.– user2357112
Nov 14 '18 at 23:15
well, mathematically speaking true, but computationally: it can inherit all the methods without any issues.
– Foad
Nov 14 '18 at 23:17
well, mathematically speaking true, but computationally: it can inherit all the methods without any issues.
– Foad
Nov 14 '18 at 23:17
add a comment |
1 Answer
1
active
oldest
votes
You clearly know how to overload operators. Use type checking to return objects of the desired type or NotImplemented in each case.
Would you be so kind to show me how it should be done? are you saying that I can put some if statements inside those methods and then return different things based on the input? probably usingisinstance
?
– Foad
Nov 14 '18 at 23:33
Exactly so. Please try that and update the question if you have more difficulties.
– roeen30
Nov 14 '18 at 23:36
add a 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%2f53310157%2fhow-to-do-operator-overloading-between-two-different-classes-in-python%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
You clearly know how to overload operators. Use type checking to return objects of the desired type or NotImplemented in each case.
Would you be so kind to show me how it should be done? are you saying that I can put some if statements inside those methods and then return different things based on the input? probably usingisinstance
?
– Foad
Nov 14 '18 at 23:33
Exactly so. Please try that and update the question if you have more difficulties.
– roeen30
Nov 14 '18 at 23:36
add a comment |
You clearly know how to overload operators. Use type checking to return objects of the desired type or NotImplemented in each case.
Would you be so kind to show me how it should be done? are you saying that I can put some if statements inside those methods and then return different things based on the input? probably usingisinstance
?
– Foad
Nov 14 '18 at 23:33
Exactly so. Please try that and update the question if you have more difficulties.
– roeen30
Nov 14 '18 at 23:36
add a comment |
You clearly know how to overload operators. Use type checking to return objects of the desired type or NotImplemented in each case.
You clearly know how to overload operators. Use type checking to return objects of the desired type or NotImplemented in each case.
answered Nov 14 '18 at 23:29
roeen30roeen30
56629
56629
Would you be so kind to show me how it should be done? are you saying that I can put some if statements inside those methods and then return different things based on the input? probably usingisinstance
?
– Foad
Nov 14 '18 at 23:33
Exactly so. Please try that and update the question if you have more difficulties.
– roeen30
Nov 14 '18 at 23:36
add a comment |
Would you be so kind to show me how it should be done? are you saying that I can put some if statements inside those methods and then return different things based on the input? probably usingisinstance
?
– Foad
Nov 14 '18 at 23:33
Exactly so. Please try that and update the question if you have more difficulties.
– roeen30
Nov 14 '18 at 23:36
Would you be so kind to show me how it should be done? are you saying that I can put some if statements inside those methods and then return different things based on the input? probably using
isinstance
?– Foad
Nov 14 '18 at 23:33
Would you be so kind to show me how it should be done? are you saying that I can put some if statements inside those methods and then return different things based on the input? probably using
isinstance
?– Foad
Nov 14 '18 at 23:33
Exactly so. Please try that and update the question if you have more difficulties.
– roeen30
Nov 14 '18 at 23:36
Exactly so. Please try that and update the question if you have more difficulties.
– roeen30
Nov 14 '18 at 23:36
add a 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%2f53310157%2fhow-to-do-operator-overloading-between-two-different-classes-in-python%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
1
Why does
vector2D
subclasspoint2D
? A vector is not a point.– user2357112
Nov 14 '18 at 23:15
well, mathematically speaking true, but computationally: it can inherit all the methods without any issues.
– Foad
Nov 14 '18 at 23:17