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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:51:06+00:00 2026-05-26T12:51:06+00:00

In the following, setattr succeeds in the first invocation, but fails in the second,

  • 0

In the following, setattr succeeds in the first invocation, but fails in the second, with:

AttributeError: 'method' object has no attribute 'i'

Why is this, and is there a way of setting an attribute on a method such that it will only exist on one instance, not for each instance of the class?

class c:

    def m(self):

        print(type(c.m))
        setattr(c.m, 'i', 0)

        print(type(self.m))
        setattr(self.m, 'i', 0)

Python 3.2.2

  • 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-26T12:51:07+00:00Added an answer on May 26, 2026 at 12:51 pm

    The short answer: There is no way of adding custom attributes to bound methods.

    The long answer follows.

    In Python, there are function objects and method objects. When you define a class, the def statement creates a function object that lives within the class’ namespace:

    >>> class c:
    ...     def m(self):
    ...         pass
    ...
    >>> c.m
    <function m at 0x025FAE88>
    

    Function objects have a special __dict__ attribute that can hold user-defined attributes:

    >>> c.m.i = 0
    >>> c.m.__dict__
    {'i': 0}
    

    Method objects are different beasts. They are tiny objects just holding a reference to the corresponding function object (__func__) and one to its host object (__self__):

    >>> c().m
    <bound method c.m of <__main__.c object at 0x025206D0>>
    >>> c().m.__self__
    <__main__.c object at 0x02625070>
    >>> c().m.__func__
    <function m at 0x025FAE88>
    >>> c().m.__func__ is c.m
    True
    

    Method objects provide a special __getattr__ that forwards attribute access to the function object:

    >>> c().m.i
    0
    

    This is also true for the __dict__ property:

    >>> c().m.__dict__['a'] = 42
    >>> c.m.a
    42
    >>> c().m.__dict__ is c.m.__dict__
    True
    

    Setting attributes follows the default rules, though, and since they don’t have their own __dict__, there is no way to set arbitrary attributes.

    This is similar to user-defined classes defining __slots__ and no __dict__ slot, when trying to set a non-existing slot raises an AttributeError (see the docs on __slots__ for more information):

    >>> class c:
    ...     __slots__ = ('a', 'b')
    ...
    >>> x = c()
    >>> x.a = 1
    >>> x.b = 2
    >>> x.c = 3
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: 'c' object has no attribute 'c'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this following python code, it works fine in python but fails with
Following this question: Good crash reporting library in c# Is there any library like
Following this tutorial http://msdn.microsoft.com/en-us/vstudio/hh543922.aspx , I'm trying to use the ReplaceNode method that should
Following chunk html code works as expected: <iframe src=http://www.amazon.com/></iframe> But when trying embed inner
Following leads in this thread and others, I've set up a code block where
Following method is accepted if I set UIColor like: [tabBarController.tabBar setSelectedImageTintColor:[UIColor greenColor]]; However I
I have the following code. class person(object): def __init__(self, keys): for item in keys:
I'm doing my first steps in Python 2.7 Metaprogramming, and wrote following code :
I saw the following code: def __init__(self, fn, **kw): [setattr(self,k,v) for (k,v) in kw.items()]
I would like to create object properties using a loop, and the setattr function.

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.