Which is more pythonic?

Discussion in 'Python' started by benjamin.zimmerman@gmail.com, Jan 25, 2008.

  1. Guest

    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
    , Jan 25, 2008
    #1
    1. Advertising

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

    On Jan 24, 2008, at 10:14 PM, wrote:

    > 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
    > --
    > http://mail.python.org/mailman/listinfo/python-list


    Travis Jensen


    http://softwaremaven.innerbrane.com/

    You should read my blog; it is more interesting than my signature.
    Travis Jensen, Jan 25, 2008
    #2
    1. Advertising

  3. Paddy Guest

    On Jan 25, 5:14 am, ""
    <> wrote:
    > 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.
    Paddy, Jan 25, 2008
    #3
  4. Paul Hankin Guest

    wrote:
    > 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.

    --
    Paul Hankin
    Paul Hankin, Jan 25, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. gabor
    Replies:
    15
    Views:
    426
    Jorgen Grahn
    Sep 17, 2005
  2. Carl J. Van Arsdall
    Replies:
    4
    Views:
    500
    Bruno Desthuilliers
    Feb 7, 2006
  3. alf
    Replies:
    11
    Views:
    459
    7stud
    May 14, 2007
  4. John Posner
    Replies:
    4
    Views:
    227
    Terry Reedy
    Apr 2, 2009
  5. Filip Gruszczyński

    Which is more pythonic?

    Filip Gruszczyński, Dec 3, 2009, in forum: Python
    Replies:
    4
    Views:
    240
Loading...

Share This Page