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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:06:01+00:00 2026-05-30T14:06:01+00:00

I would like to expose the methods of a class as functions (after decoration)

  • 0

I would like to expose the methods of a class as functions (after decoration) in my local scope. For example if I had a class and decorator:

def some_decorator(f):
    ...transform f...
    return decorated_f

class C(object):
    def __init__(self,x):
        self.x = x
    def f(self,y):
        "Some doc string"
        return self.x + y
    def g(self,y,z):
        "Some other doc string"
        return self.x + y + z

and if I didn’t care about automizing the process I could add the following code to my module the following:

@some_decorator
def f(C_instance,x):
    "Some doc string."
    return C_instance.f(x)

@some_decorator
def g(C_instance,x,y):
    "Some other doc string."
    return C_instance.g(x,y)    

to the effect that the following evaluate to True

c = C(0)
f(c,1) == c.f(1)

But I would like to be able to do this automatically. Something like:

my_funs = expose_methods(MyClass)
for funname, fun in my_funs.iteritems():
    locals()[funname] = some_decorator(fun)

foo = MyClass(data)
some_functionality(foo,*args) == foo.some_functionality(*args)

would do the trick (although it feels a little wrong declaring local variables this way). How can I do this in a way so that all the relevant attributes of the method correctly transform into the function versions? I would appreciate any suggestions.

P.S.

I am aware that I can decorate methods of class instances, but this is not really what I am after. It is more about (1) a preference for the function version syntax (2) the fact that my decorators make functions map over collections of objects in fancy and optimized ways. Getting behavior (2) by decorating methods would require my collections classes to inherit attributes from the objects they contain, which is orthogonal to the collection semantics.

  • 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-30T14:06:03+00:00Added an answer on May 30, 2026 at 2:06 pm

    Are you aware that you can use the unbound methods directly?

    obj.foo(arg) is equivalent to ObjClass.foo(obj, arg)

    class MyClass(object):
        def foo(self, arg):
            ...
    
    obj = MyClass()
    print obj.foo(3) == MyClass.foo(obj, 3) # True
    

    See also Class method differences in Python: bound, unbound and static and the documentation.

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

Sidebar

Related Questions

How would one expose static variables like this class MyClass: X = 1 Y
I have an abstract class which I would like to be able to expose
I have a Silverlight ModelViewViewModel project that I would like to expose a property
I'm building an ASP.NET 3.5 application and would like to expose an enum I
I would like to expose a function that can take an optional anonymous method
For a server-side plugin framework I would like to implement DLLs which expose a
I have some utilities and extension methods that I would like to leverage in
I would like to be able to achieve something like this: class Zot {
I would like to prevent a property from being exposed via my WCF web
We've got a bunch of data that we'd like to expose to the world

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.