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

The Archive Base Latest Questions

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

Is there a way to call the method of a class from another class?

  • 0

Is there a way to call the method of a class from another class? I am looking for something like PHP’s call_user_func_array().
Here is what I want to happen:

class A:
    def method1(arg1, arg2):
        ...

class B:
    A.method1(1, 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-17T15:06:27+00:00Added an answer on May 17, 2026 at 3:06 pm

    update: Just saw the reference to call_user_func_array in your post. that’s different. use getattr to get the function object and then call it with your arguments

    class A(object):
        def method1(self, a, b, c):
            # foo
    
    method = A.method1
    

    method is now an actual function object. that you can call directly (functions are first class objects in python just like in PHP > 5.3) . But the considerations from below still apply. That is, the above example will blow up unless you decorate A.method1 with one of the two decorators discussed below, pass it an instance of A as the first argument or access the method on an instance of A.

    a = A()
    method = a.method1
    method(1, 2)
    

    You have three options for doing this

    1. Use an instance of A to call method1 (using two possible forms)
    2. apply the classmethod decorator to method1: you will no longer be able to reference self in method1 but you will get passed a cls instance in it’s place which is A in this case.
    3. apply the staticmethod decorator to method1: you will no longer be able to reference self, or cls in staticmethod1 but you can hardcode references to A into it, though obviously, these references will be inherited by all subclasses of A unless they specifically override method1 and do not call super.

    Some examples:

    class Test1(object): # always inherit from object in 2.x. it's called new-style classes. look it up
        def method1(self, a, b):
            return a + b
    
        @staticmethod
        def method2(a, b):
            return a + b
    
        @classmethod
        def method3(cls, a, b):
            return cls.method2(a, b)
    
    t = Test1()  # same as doing it in another class
    
    Test1.method1(t, 1, 2) #form one of calling a method on an instance
    t.method1(1, 2)        # form two (the common one) essentially reduces to form one
    
    Test1.method2(1, 2)  #the static method can be called with just arguments
    t.method2(1, 2)      # on an instance or the class
    
    Test1.method3(1, 2)  # ditto for the class method. It will have access to the class
    t.method3(1, 2)      # that it's called on (the subclass if called on a subclass) 
                         # but will not have access to the instance it's called on 
                         # (if it is called on an instance)
    

    Note that in the same way that the name of the self variable is entirely up to you, so is the name of the cls variable but those are the customary values.

    Now that you know how to do it, I would seriously think about if you want to do it. Often times, methods that are meant to be called unbound (without an instance) are better left as module level functions in python.

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

Sidebar

Related Questions

Is there a way to pass a call back function in a Java method?
Is there a way to create C# objects and call methods from unmanaged C++,
Is there a way to call an external script or program from Flash CS3
Is there a better way to call MSBuild from C#/.NET than shelling out to
Is there any way to have PHP automatically call a function, before a script
Is there a way to get the responde headers from a web service call
Is there a way to delete a folder, lets call it FolderX from an
Is there any way to secure your assembly down to the class/property & class/method
Is there a way to call Static Classes / Methods by name? Example: $name
Is there a way to call an EJB that is served through WebSphere (iiop://host:port/ejbName)

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.