I want to create a class with two methods at this point (I also want to be able to
alter the class obviously).
class ogrGeo(object):
def __init__(self):
pass
def CreateLine(self, o_file, xy):
#lots of code
def CreatePoint(self, o_file, xy):
# lot's of the same code as CreateLine(),
# only minor differences
To keep things as clean and to to repeat as
less code as possible I’m asking for some advise. The two methods CreateLine()
and CreatePoint() share a lot of code. To reduce redundance:
Should a define third method that both methods can call?
In this case you could still call
o = ogrGeo()seperatly.
o.CreateLine(...)
o.CreatePoint(...)
Or should I merge them into one method? Is there another solution I haven’t thought about or know nothing about?
Thanks already for any suggestions.
It’s perfectly normal to factor out common code into a (private) helper method:
The method could return a value, or it could directly manipulate the attributes on
self.A word of advice: read the Python style guide and stick to it’s conventions. Most other python projects do, and it’ll make your code easier to comprehend for other Python developers if you do.