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

  • SEARCH
  • Home
  • 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 7922015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T16:48:25+00:00 2026-06-03T16:48:25+00:00

Right now, I have the following class methods: def check_capacity(self, at_index) def update_capacity(self, at_index)

  • 0

Right now, I have the following class methods:

def check_capacity(self, at_index)
def update_capacity(self, at_index)

The former returns a Boolean, while the latter changes an instance variable. The problem is both methods do very similar things. I feel like I’m violating DRY?

I’d like to have one method:

def update_capacity(self, at_index)

which I can use as:

if update_capacity(at_index):

that would create the intended side-effects if the side-effects are desirable, and return False otherwise.

My attempt was to copy the instance variable, check to see if the copy was desirably changed, and then set the instance variable to the copy if correct and return True, or don’t and return False otherwise. However, this doesn’t work with mutable data structures (like lists)!

Should I just be doing this with a “deep copy”? Or is there a better way to go about doing this? I’d like to be as Pythonic as possible.

EDIT

The check_capacity iterates through the instance variable and checks if making the change would violate a condition.

The update_capacity iterates through the instance variable and makes the change, knowing the condition won’t be violated.

Both have very similar code.

  • 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-03T16:48:27+00:00Added an answer on June 3, 2026 at 4:48 pm

    I have a hunch that those two functions together manage to straddle your problem without exactly hitting it on the head. If I have this right, you want to have update_capacity either change something or return False if changing something is not desired.

    It seems to me that you will be able to achieve this functionality by adding the checking mechanism from check_capacity into update_capacity, performing the condition check before executing the body of update_capacity:

    def update_capacity(self, at_index):
        if <condition from check_capacity>:
            <body of update_capacity>
        else:
            return False
    

    Of course, this code will return None if the condition is true, so if you want to keep your function signatures tidy, you could return True or something depending on what fits the rest of your code.

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

Sidebar

Related Questions

I have the following class: class Enemy(pygame.sprite.Sprite): def __init__(self): pygame.init() pygame.sprite.Sprite.__init__(self) # Basic variables
Right now I have the following code working: @UiHandler(usernameTextBox) void onUsernameTextBoxKeyPress(KeyPressEvent event) { keyPress(event);
I want to match dates that have the following format: 2010-08-27, 2010/08/27 Right now
Right now I have an upload field while uploads files to the server. The
Right now I have a function, in a class that is used to listen
I am working on game engine prototype and have the following question: Right now
I am experimenting with Groovy closures and delegates right now. I have the following
I have the following code: class Engine attr_accessor :isRunning def initialize @isRunning = false
So basically, the below is the code I have right now: class MY_Log extends
Good day! I right now have a function the drags an element from a

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.