Python refactoring (similar methods in class)?

First, standard practice is to capitalize classes (so Point not point ). I'd make use of the add and sub (and possibly iadd and isub ) methods, as well. A first cut might look like this.

First, standard practice is to capitalize classes (so Point, not point). I'd make use of the __add__ and __sub__ (and possibly __iadd__ and __isub__) methods, as well. A first cut might look like this: class Point(object): def __init__(self, x, y): self.

X = x self. Y = y def __add__(self, p): return Point(self. X + p.

X, self. Y + p. Y) def __sub__(self, p): return Point(self.

X - p. X, self. Y - p.

Y) I know you're looking to pull the logic out into a single method, something like: class Point(object): def __init__(self, x, y): self. X = x self. Y = y def _adjust(self, x, y): return Point(self.

X + x, self. Y + y) def __add__(self, p): return self. _adjust(p.

X, p. Y) def __sub__(self, p): return self. _adjust(-p.

X, -p. Y) ... but that seems more complicated, without much gain.

This is what I was looking for. I didn't know that python had the add and sub. Also the underscore in front of _adjust, what is the meaning and convention for that?

I agree that adding an adjust method is more complicated without gain. Thanks for this response. – user221014 Jun 30 '10 at 20:04 1 The underscore in _adjust is simply a Python convention that the method is "semi-private".

There's virtually no difference, except that it signals to others reading the code they should probably avoid calling it directly unless they know what they're doing. Note: this is not the case for a double-underscore (like __adjust). See docs.python.Org/tutorial/classes.

Html#private-variables for details. – Chris B. Jun 30 '10 at 20:09 Thanks for the help!

– user221014 Jun 30 '10 at 20:13.

What about that: import operator class point(object): def __init__( self, x, y ): self. X, self. Y = x, y def _do_op(self, op, p): x = op(self.

X, p. X) y = op(self. Y, p.

Y) return point(x, y) def add( self, p ): return self. _do_op(operator. Add, p) def sub( self, p ): return self.

_do_op(operator. Sub, p).

This was what I came up with. While it doesn't really gain you much in this simple example, it DOES allow you to internally hide the representation of the point. The methods don't care what variables are involved in adding or subtracting, just that it should be done to the point.

– stw_dev Jun 30 '10 at 20:07.

Here's something you could do. Def __add__(self, p): # used this so that you can add using the + operator x = self. X + p.

X y = self. Y + p. Y return point(x, y) def __sub__(self, p): return self + point(-p.

X, -p. Y).

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions