0

I'm doing some editing recently & normally when I wanted to save time I'd wrap a function around a more generic function. I'm also reading more on OO maintenance and they state I shouldn't be letting an object know the name of another object. I am using functions in the the following:

def getWidth(self, width_id):

    retrieve_characteristics(self, variable_id, variable_characteristic)
    try: return self.c.fetchone()[0]
    except: return None


def retrieve_characteristics(self, variable_id, variable_characteristic):
    self.c.execute('SELECT ' + variable_characteristic+ ' FROM ' + variable_characteristic+ ' WHERE id=?',(variable_id,))

My question being - does this apply to functions like this or is there a better way of implementing dry? I have 4 other functions in my base python init which would be using retrieve_characteristics.

Bonus points if anyone has comments on if I can shove the try & except code into the retrieve_characteristics without weird inherent errors later :)

Thanks!

Mirv - Matt
  • 553
  • 7
  • 22
  • 1
    Are these class methods or procedural functions? If it's just plain functions, avoid using `self` as it is confusing for whoever reads the code. One issue is that `getWidth` uses variables `characteristic` and `id` which seem to be global ones in the context of that method. Where do they come from? Can they be factored into the object you're dealing with? If you use `getWidth` several times in the lifecycle of your object instance, and because it hits the database, you may want to make it a "cached property" of your object. I think your question can't be answered without a broader context. – raphv Jun 03 '16 at 12:37
  • Also, they are housed in the __init__.py for now. – Mirv - Matt Jun 03 '16 at 12:51
  • There's still a big inconsistency between the two functions because variable_id and variable_characteristic is a global in the first case and a function argument in the second case. Also, what is "self" if they are not object methods?? And why aren't they object methods, by the way? – raphv Jun 03 '16 at 12:57
  • It's a first project in python & flask, that's why. When I'm looking back - I apologize - they are actually already in the shoe object. I'm scrolling too fast on this one. Originally they were stand alone. – Mirv - Matt Jun 03 '16 at 13:56
  • 1
    Your try/except clause is overly broad, which is will bite you later (https://realpython.com/blog/python/the-most-diabolical-python-antipattern/) – jaap3 Jun 03 '16 at 18:20
  • Great read, will be going back there! Ty! – Mirv - Matt Jun 06 '16 at 12:26

0 Answers0