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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:27:39+00:00 2026-05-13T19:27:39+00:00

I have a big library written in C++ and someone created an interface to

  • 0

I have a big library written in C++ and someone created an interface to use it in python (2.6) in an automatic way. Now I have a lot of classes with getter and setter methods. Really: I hate them.

I want to re-implement the classes with a more pythonic interface using properties. The problem is that every class has hundreds of getters and setters and I have a lot of classes. How can I automatically create properties?

For example, if I have a class called MyClass with a GetX() and SetX(x), GetY, SetY, etc… methods, how can I automatically create a derived class MyPythonicClass with the property X (readable if there is the getter and writable if there is the setter), and so on? I would like a mechanism that lets me to choose to skip some getter/setter couples where it is better to do the work by hand.

  • 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-05-13T19:27:40+00:00Added an answer on May 13, 2026 at 7:27 pm

    Here’s a way to do it with a class decorator

    def make_properties(c):
        from collections import defaultdict
        props=defaultdict(dict)
        for k,v in vars(c).items():
            if k.startswith("Get"):
                props[k[3:]]['getter']=v
            if k.startswith("Set"):
                props[k[3:]]['setter']=v
        for k,v in props.items():
            setattr(c,k,property(v.get('getter'),v.get('setter')))
        return c
    
    @make_properties
    class C(object):
        def GetX(self):
            print "GetX"
            return self._x
    
        def SetX(self, value):
            print "SetX"
            self._x = value
    
    c=C()
    c.X=5
    c.X
    

    Here is a slightly more complicated version that allows you to specify a list of items to skip

    def make_properties(skip=None):
        if skip is None:
            skip=[]
        def f(c):
            from collections import defaultdict
            props=defaultdict(dict)
            for k,v in vars(c).items():
                if k.startswith("Get"):
                    props[k[3:]]['getter']=v
                if k.startswith("Set"):
                    props[k[3:]]['setter']=v
            for k,v in props.items():
                if k in skip:
                    continue
                setattr(c,k,property(v.get('getter'),v.get('setter')))
            return c
        return f
    
    @make_properties(skip=['Y'])
    class C(object):
        def GetX(self):
            print "GetX"
            return self._x
    
        def SetX(self, value):
            print "SetX"
            self._x = value
    
        def GetY(self):
            print "GetY"
            return self._y
    
        def SetY(self, value):
            print "SetY"
            self._y = value
    
    c=C()
    c.X=5
    c.X
    c.Y=5
    c.Y
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I used Ribbon library for WPF for a big project. Now when I have
I am a big fan of the Lightbox2 library, and have used it in
I have big issue with url-rewriting for IIS 7.0. I've written simple module for
I have a big red button and I'm trying to use javascript to perform
I have a native dynamic link library written in VC++6 and I do not
I have developed a big library of functions in R. For the moment I
How would you maintain the legacy applications that: Has no unit tests have big
I have a big string (let's call it a CSV file, though it isn't
i have a big web application running in perl CGI. It's running ok, it's
I have a big load of documents, text-files, that I want to search for

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.