How to do operator overloading between two different classes in python












1















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.*.










share|improve this question




















  • 1





    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


















1















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.*.










share|improve this question




















  • 1





    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
















1












1








1








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.*.










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 23:17







Foad

















asked Nov 14 '18 at 23:13









FoadFoad

1,47521130




1,47521130








  • 1





    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
















  • 1





    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










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














1 Answer
1






active

oldest

votes


















0














You clearly know how to overload operators. Use type checking to return objects of the desired type or NotImplemented in each case.






share|improve this answer
























  • 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











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
});


}
});














draft saved

draft discarded


















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









0














You clearly know how to overload operators. Use type checking to return objects of the desired type or NotImplemented in each case.






share|improve this answer
























  • 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
















0














You clearly know how to overload operators. Use type checking to return objects of the desired type or NotImplemented in each case.






share|improve this answer
























  • 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














0












0








0







You clearly know how to overload operators. Use type checking to return objects of the desired type or NotImplemented in each case.






share|improve this answer













You clearly know how to overload operators. Use type checking to return objects of the desired type or NotImplemented in each case.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 14 '18 at 23:29









roeen30roeen30

46629




46629













  • 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



















  • 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

















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




















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python