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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:26:08+00:00 2026-06-17T08:26:08+00:00

I am working on some legacy code (created by someone in love with spaghetti

  • 0

I am working on some legacy code (created by someone in love with spaghetti code) that has over 150 getters and over 150 setters. The getters look like this:

def GetLoadFee(self):
    r_str = ""
    if len(self._LoadFee) > 20:
        r_str = self._LoadFee[:20]
    else:
        r_str = self._LoadFee.strip()
    return r_str.strip()

def GetCurrency(self):
    r_str = ""
    if len(self._Currency) > 3:
        r_str = self._Currency[:3]
    else:
        r_str = self._Currency.strip()
    return r_str.strip()

I would love to take the contents of each of these Getters and put them into a decorator /closure or some other method to make this code easier to maintain. The Setters are all one liners, so they’re not as important. But they are basically all the same too. Any ideas to make this less painful?

NOTE: I still need the original Getter names as they are used in other programs as this nasty script is used in lots of other legacy 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-17T08:26:09+00:00Added an answer on June 17, 2026 at 8:26 am
    def make_generic_getter(name, maxlen):
        def getter(self):
            value = getattr(self, name)
            r_str = ""
            if len(value) > maxlen:
                r_str = value[:maxlen]
            else:
                r_str = value.strip()
            return r_str.strip()
        return getter
    

    Now, you can do this:

    class Foo(object):
        def __init__(self):
            self._Bar = 'abc'
            self._Baz = 'def'
        GetBar = make_generic_getter('_Bar', 5)
        GetBaz = make_generic_getter('_Baz', 2)
    

    Then:

    >>> f = Foo()
    >>> f.GetBar()
    'abc'
    >>> f.GetBaz()
    'de'
    

    Clearly, there’s also a lot of repetitive and unnecessary stuff in the original function. (And it would be much better to use PEP8-style names for your properties.) But obviously it’s much easier to refactor first, then improve, than the other way around. (In other words, start here, but don’t stop here.)

    From the comments:

    How does the method maker get the “self” reference?

    The method maker doesn’t actually get the self reference. There is no self reference to get at the time the method maker is being called. But there also is no self reference to get for a normal method at the time the class is being defined. In either case, you’re just defining a function that takes self as its first parameter, and it somehow magically gets the appropriate self when you call it.

    To really understand how this actually works, you need to know about descriptors. See Implementing Descriptors and Invoking Descriptors (or the 3.3 version), read it over a few times, look at how the @property decorator is implemented, play around in the interactive interpreter, give up, go to sleep, and try again tomorrow, and it should all click. But it’s easier if you learn the magic version first, so let’s do that, using a simpler example:

    >>> def func(self): pass
    >>> class C(object):
    ...     def meth(self): pass
    ...     fake1 = func
    >>> C.fake2 = func
    >>> func, C.meth, C.fake1, C.fake2
    (<function __main__.func>, <unbound method C.meth>, <unbound method C.func>, <unbound method C.func>)
    

    An unbound method is just a thing with an im_class holding its class, an im_func holding a normal function, and an im_self holding None. And when you do fake1 = func in the class definition, or C.fake2 = func after the fact, you don’t actually end up with func itself as the value of fake1 or fake2, but with an unbound method wrapped around func, its im_class pointing at C.

    >>> c = C()
    >>> c.meth, c.fake1
    (<bound method C.meth of <__main__.C object at 0x111ebb0d0>>, <bound method C.meth of <__main__.C object at 0x111ebb0d0>>)
    

    When you take an instance of a class, all of its unbound methods become bound methods. If you look at the bound methods’ attributes, they’re the same as the unbound methods, except that im_self is c instead of None. And when you call c.fake1(), that’s how it works—Python sees that c.fake1 is a bound method, so, in effect, it calls c.fake1.im_func(c.fake1.im_self). And that’s how fake gets its self parameter.

    (This all becomes simpler in Python 3, because there’s no such thing as unbound methods anymore, but I assume you care more about Python 2 given that you’re dealing with a huge mess of legacy code.)

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

Sidebar

Related Questions

I'm working with some legacy code that has an import like so: #import C:\Program
Probably a novice question: I'm working with some legacy code that has an implementation
I'm working updating some legacy code that does not properly handle user input. The
I am working on an application which has some legacy code. Here, there is
Im working with some legacy code that generates a given message on a webpage
I'm working on some legacy code that uses win32 WriteFile() to write to a
I'm working with some legacy code that makes extensive use of this kind of
Hi I'm working on some legacy code that goes something along the lines of
There is the following definition in some legacy code that I am working with.
Some background first: I am working on some legacy code that implements UI interfaces.

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.