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 1023235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:35:08+00:00 2026-05-16T11:35:08+00:00

Ok I have some code that boils down to a pattern like this class

  • 0

Ok I have some code that boils down to a pattern like this

class Foo(object):
def get_something1(self):
    # in the actual code it asks a web server but here I replaced that with "foo_something1" instead of the code from a web server
    self.something1 = "foo_something1"

def get_something2(self):
    # needs the result of get_something1; right now I have it get it by instance variable like so
    self.something2 = self.something1 + "_something_2"

My question is, should I do the above (methods getting what they need by instance variables) or by taking arguments like so; also should I return something1 and something2 in the get_* methods?

class Foo(object):
def get_something1(self):
    # in the actual code it asks a web server but here I replaced that with "foo_something1" instead of the code from a web server
    self.something1 = "foo_something1"

def get_something2(self, something_1):
    # needs the result of get_something1; right now I have it get it by instance variable like so
    self.something2 = something1 + "_something_2"

Now in my code the methods return deferreds (twisted); Right now when the deferreds fire it sets the instance variables and returns None; In the actual code I have another function that checks to see if something1 or something2 is expired; I update them which brings up a problem because I update it like so…

d = self.get_something1()
##d.addCallback(self.get_something2) # errors because it gets the argument None (so I do below)
d.addCallback(lambda ignored: self.get_something2())# problem the deferred for get_something2 is inaccessible

So right now the update code is either ugly, error filled, or both. So I have a feeling that I am doing something either a) un pythonic or b) un twisted or c) some other bad design.


Thanks for the answers, but those won’t work because the functions return deferreds and not the actual value (other wise it would have to block).

  • 1 1 Answer
  • 1 View
  • 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-05-16T11:35:09+00:00Added an answer on May 16, 2026 at 11:35 am

    Don’t use get_* methods. In Python the better way is to use properties:

    class Foo(object):
        @property
        def something1(self):
            # in the actual code it asks a web server but here I replaced that with "foo_something1" instead of the code from a web server
            return "foo_something1"
        @property
        def something2(self):
            # needs the result of get_something1; right now I have it get it by instance variable like so
            return self.something1 + "_something_2"
    

    Note that when something1 is a property, self.something1 (without parentheses!) calls the corresponding function Foo.something1.

    Once you have working code, you can see how something2 is being used. If you have both

        @property
        def something2(self):
            return self.something1 + "_something_2"
    
        @property
        def something3(self):
            return self.otherthing1 + "_something_2"
    

    Then you might want to refactor the code to use

        def something2(self,prefix):
            return prefix+"_something_2"
    

    We don’t know enough about your situation to tell you which is better. But the answer may become obvious to you once you see the use patterns of your working code.

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

Sidebar

Related Questions

I have some code that, for the purposes of this question, boils down to
I have some code that looks like this: foreach(var obj in collection) { try
I have code that boils down to the following: template <typename T> struct Foo
I have a a function, that essentially boils down to this (the part that
I came across some code that boils down to the following: enum BAR {
I strongly suspect that this boils down to me needing to understand some language
I have some code that will change the background color of a specific label
I have some code that is supposed to return an NSString. Instead it is
I have some code that generates Visio masters for me, and some masters have
I have some code that causes the box2d physics simulation to stutter forever after

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.