Which is more pythonic?

B

benjamin.zimmerman

I have a goal function that returns the fitness of a given solution. I
need to wrap that function with a class or a function to keep track of
the best solution I encounter. Which of the following would best serve
my purpose and be the most pythonic?

class Goal:
def __init__(self, goal):
self.goal = goal
self.best = None
self.best_score = None

def __call__(self, solution):
score = self.goal(solution)
if self.best is None or score > self.best_score:
self.best_score = score
self.best = solution
return score

def save_best_goal(goal):
def new_goal(solution):
score = goal(solution)
if new_goal.best is None or score > new_goal.best_score:
new_goal.best = solution
new_goal.best_score = score
return score
new_goal.best = new_goal.best_score = None
return new_goal

-Ben
 
T

Travis Jensen

Well, regardless of being "pythonic" or not, the first is far more
understandable and therefore more maintainable. Objects were invented
to handle holding state; using a function to hold state is, in my
opinion, doing a language-based cheat. :)

tj

I have a goal function that returns the fitness of a given solution. I
need to wrap that function with a class or a function to keep track of
the best solution I encounter. Which of the following would best serve
my purpose and be the most pythonic?

class Goal:
def __init__(self, goal):
self.goal = goal
self.best = None
self.best_score = None

def __call__(self, solution):
score = self.goal(solution)
if self.best is None or score > self.best_score:
self.best_score = score
self.best = solution
return score

def save_best_goal(goal):
def new_goal(solution):
score = goal(solution)
if new_goal.best is None or score > new_goal.best_score:
new_goal.best = solution
new_goal.best_score = score
return score
new_goal.best = new_goal.best_score = None
return new_goal

-Ben

Travis Jensen
(e-mail address removed)

http://softwaremaven.innerbrane.com/

You should read my blog; it is more interesting than my signature.
 
P

Paddy

I have a goal function that returns the fitness of a given solution. I
need to wrap that function with a class or a function to keep track of
the best solution I encounter. Which of the following would best serve
my purpose and be the most pythonic?

class Goal:
def __init__(self, goal):
self.goal = goal
self.best = None
self.best_score = None

def __call__(self, solution):
score = self.goal(solution)
if self.best is None or score > self.best_score:
self.best_score = score
self.best = solution
return score

def save_best_goal(goal):
def new_goal(solution):
score = goal(solution)
if new_goal.best is None or score > new_goal.best_score:
new_goal.best = solution
new_goal.best_score = score
return score
new_goal.best = new_goal.best_score = None
return new_goal

-Ben

You could also wrap the best score logic as a decorator applied to a
function to save the best score as a function attribute. Whilst not
class based, Python isn't solely an OO language. but if you know that
extra capabilities are going to be needed you might favour the class
based solution.

- Paddy.
 
P

Paul Hankin

I have a goal function that returns the fitness of a given solution. I
need to wrap that function with a class or a function to keep track of
the best solution I encounter. Which of the following would best serve
my purpose and be the most pythonic?

You could write a function that generates your trial solutions and use
max.

Eg: instead of using your Goal class:

def f(score_function):
goal = Goal(score_function)
while there's more solutions:
... produce trial solution
goal(trial_solution)
return goal.best

Write a solution generator...
def all_solutions():
while there's more solutions:
... produce trial solution
yield trial_solution

....and find the best using
best = max(all_solutions(), key = score_function)

This uses a builtin rather than either of the snippets in the original
post, and it's also a much cleaner approach.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,836
Latest member
BuyBlissBitesCBD

Latest Threads

Top