Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8032853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:31:14+00:00 2026-06-05T01:31:14+00:00

So I need to mutate a method at object initialization based upon input to

  • 0

So I need to mutate a method at object initialization based upon input to __init__ (for those interested, I’m changing the navigate_to method in a test framework based upon which type of automator is being instantiated (Selenium, mobile device automator, etc)). I’ve come up with a solution using a conditionally created closure in __init__, but it seems like there should be a more elegant and optimized way to do this. As an example of the approach:

class Foo(object):
    def __init__(self, x):
        self.x = x
        if x % 2:
            def odd_or_even():
                return '%d odd' % self.x
        else:
            def odd_or_even():
                return '%d even' % self.x
        self.odd_or_even = odd_or_even

Resulting in:

>>> foo1 = Foo(1)
>>> foo2 = Foo(2)
>>> foo1.odd_or_even()
'1 odd'
>>> foo2.odd_or_even()
'2 even'

This works, but I feel like there should be some better way to do this. Suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T01:31:16+00:00Added an answer on June 5, 2026 at 1:31 am

    I would make a different method for each type of automator, and then a generic method that uses the state of self to determine which of the specific methods to call.

    Why do you need to create a closure containing the decision, when you can just record the decision in self?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Need to call a filter function on some options based on a radio selected
I'm working on a dropdown menu and need to mutate the existing html markup
I need to build an XML document from a Java object hierarchy. Both the
Need help with copying array object to a temp array object using a for
I try to find object in my array and if success I need to
Need to insert selected text on the page into textarea. There must be some
need to add a 2nd css stylesheet to a page. do i add a
Need some help, please. I have a line of horizontal thumbnails loaded as ONE
Need to take a SELECT drop down list options and find if any of
Need to use own imaged markers instead built-in pins. I have several questions. 1.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.