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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:47:06+00:00 2026-05-19T01:47:06+00:00

I’m having some issues with the self parameter, and some seemingly inconsistent behavior in

  • 0

I’m having some issues with the self parameter, and some seemingly inconsistent behavior in Python is annoying me, so I figure I better ask some people in the know. I have a class, Foo. This class will have a bunch of methods, m1, through mN. For some of these, I will use a standard definition, like in the case of m1 below. But for others, it’s more convinient to just assign the method name directly, like I’ve done with m2 and m3.

import os

def myfun(x, y):
    return x + y

class Foo():
    def m1(self, y, z):
        return y + z + 42

    m2 = os.access
    m3 = myfun

f = Foo()
print f.m1(1, 2)
print f.m2("/", os.R_OK)
print f.m3(3, 4)

Now, I know that os.access does not take a self parameter (seemingly). And it still has no issues with this type of assignment. However, I cannot do the same for my own modules (imagine myfun defined off in mymodule.myfun). Running the above code yields the following output:

3
True
Traceback (most recent call last):
  File "foo.py", line 16, in <module>
    print f.m3(3, 4)
TypeError: myfun() takes exactly 2 arguments (3 given)

The problem is that, due to the framework I work in, I cannot avoid having a class Foo at least. But I’d like to avoid having my mymodule stuff in a dummy class. In order to do this, I need to do something ala

def m3(self,a1, a2):
    return mymodule.myfun(a1,a2)

Which is hugely redundant when you have like 20 of them. So, the question is, either how do I do this in a totally different and obviously much smarter way, or how can I make my own modules behave like the built-in ones, so it does not complain about receiving 1 argument too many.

  • 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-19T01:47:07+00:00Added an answer on May 19, 2026 at 1:47 am

    I just want to add that the behaviour is not inconsistent as already Luke hinted.

    Just try the following

    print Foo.__dict__
        {'__doc__': None,
         '__module__': '__main__',
         'm1': <function m1 at 0x02861630>,
         'm2': <built-in function access>,
         'm3': <function myfun at 0x028616F0>}
    

    Here you can see that Python can’t distinguish between m1 and m2.
    That’s why both are evaluated to a bound-method.

    A bound method is something like a method with an additional first argument pointing to an object: self.m(1, 2) -> m(self, 1, 2)

    This binding behaviour is only implemented for User-defined methods. That explains why self.m2("/", os.R_OK) is not evaluated to m2(self, "/", os.R_OK).

    One last demo:

    print Foo.m1
        <unbound method Foo.m1>
    print Foo.m2
        <built-in function access>
    print f.m1
        <bound method Foo.m1 of <__main__.Foo instance at 0x02324418>>
    print f.m2
        <built-in function access>
    

    Further information about the different function types can be found here:

    http://docs.python.org/reference/datamodel.html

    And as mentioned before this binding mechanism can also be prevented by using a staticmethod descriptor:

    http://docs.python.org/library/functions.html#staticmethod

    • 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.