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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:54:00+00:00 2026-05-16T05:54:00+00:00

I understand that functions can have attributes. So I can do the following: def

  • 0

I understand that functions can have attributes. So I can do the following:

def myfunc():
    myfunc.attribute += 1
    print(myfunc.attribute)

myfunc.attribute = 1

Is it possible by any means to make such a function behave as if it were an instance? For example, I’d like to be able to do something like this:

x = clever_wrapper(myfunc)
y = clever_wrapper(myfunc)
x.attribute = 5
y.attribute = 9
x()   # I want this to print 6 (from the 5 plus increment)
y()   # I want this to print 10 (from the 9 plus increment)

As it stands, there is only one “instance” of the function, so attribute only exists once. Modifying it by either x or y changes the same value. I’d like each of them to have their own attribute. Is that possible to do at all? If so, can you provide a simple, functional example?

It is important that I be able to access attribute from inside of the function but have the value of attribute be different depending on which “instance” of the function is called. Essentially, I’d like to use attribute as if it were another parameter to the function (so that it could change the behavior of the function) but not pass it in. (Suppose that the signature of the function were fixed so that I cannot change the parameter list.) But I need to be able to set the different values for attribute and then call the functions in sequence. I hope that makes sense.

The main answers seem to be saying to do something like this:

class wrapper(object):
    def __init__(self, target):
        self.target = target
    def __call__(self, *args, **kwargs):
        return self.target(*args, **kwargs)

def test(a):
    return a + test.attribute

x = wrapper(test)
y = wrapper(test)
x.attribute = 2
y.attribute = 3
print(x.attribute) 
print(y.attribute)
print(x(3))
print(y(7))

But that doesn’t work. Maybe I’ve done it incorrectly, but it says that test does not have attribute. (I’m assuming that it’s because wrapper actually has the attribute.)

The reason I need this is because I have a library that expects a function with a particular signature. It’s possible to put those functions into a pipeline of sorts so that they’re called in order. I’d like to pass it multiple versions of the same function but change their behavior based on an attribute’s value. So I’d like to be able to add x and y to the pipeline, as opposed to having to implement a test1 function and a test2 function that both do almost exactly the same thing (except for the value of the attribute).

  • 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-16T05:54:01+00:00Added an answer on May 16, 2026 at 5:54 am

    You can make a class with a __call__ method which would achieve a similar thing.

    Edit for clarity: Instead of making myfunc a function, make it a callable class. It walks like a function and it quacks like a function, but it can have members like a class.

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

Sidebar

Related Questions

No related questions found

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.