Reference instance method outside class definition

Evan Gunter

I'm trying to pass a method as an argument outside of the definition of a class. However, since it's not defined in that scope, it doesn't work. Here's what I'd like to do:

def applyMethod(obj, method):
    obj.method()

class MyClass():
    def myMethod(self):
        print 1

a = MyClass()    

#works as expected
a.myMethod()

#"NameError: name 'myMethod' is not defined"
applyMethod(a, myMethod)
James

myMethod is only defined in the namespace of MyClass. Your code could look like this:

def applyMethod(obj, method):
    method(obj)

class MyClass():
    def myMethod(self):
        print 1

a = MyClass()    

a.myMethod()

applyMethod(a, MyClass.myMethod)

Now you're referencing myMethod from the namespace it exists in, and calling the equivalent of obj.myMethod() from the applyMethod function.

That's all you need - the instance.method() is just syntactic sugar for ClassName.method(instance), so I just rewrote the applyMethod function to work without the syntactic sugar, i.e. be passed in as the raw MyClass.myMethod, then gave it an instance of MyClass as its first argument. This is what the a.myMethod() syntax is doing in the backend.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Beginner problem. Cannot access class instance methods outside main method?

From Dev

Create a blank class instance without class definition

From Dev

Nested class definition outside outer class's, while outer class contains instance of inner class

From Dev

Python - declare method name in dictionary with method definition in outside class

From Dev

Is method a class method or instance method

From Dev

Can I reference class attribute of sub-class in instance method?

From Dev

static_assert inside/outside class definition

From Dev

Variable assignment outside class definition

From Dev

Mocking a method outside of a class

From Dev

Move method definition for template nested class outside declaration

From Dev

Declare method outside of class

From Dev

Partial Template specialization definition outside of class definition

From Dev

Java8: Is there a way to get an instance method reference from a class method reference?

From Dev

friend not allowed outside of a class definition

From Dev

How to reference the instance of the outside class in Kotlin?

From Dev

TypeScript - How to add a method outside the class definition

From Dev

inner class method definition outside of a template class

From Dev

Method reference to instance method from class type vs Method reference to instance method from instance

From Dev

Beginner problem. Cannot access class instance methods outside main method?

From Dev

Python - declare method name in dictionary with method definition in outside class

From Dev

Is method a class method or instance method

From Dev

How to reference an instance method in another instance method of a different class?

From Dev

Dynamic method definition in a class

From Dev

regarding a method definition in a class

From Dev

Partial Template specialization definition outside of class definition

From Dev

Instance created before method definition

From Dev

TypeScript - How to add a method outside the class definition

From Dev

Java 8 method reference to class instance method NPE

From Dev

Move function outside class definition

Related Related

  1. 1

    Beginner problem. Cannot access class instance methods outside main method?

  2. 2

    Create a blank class instance without class definition

  3. 3

    Nested class definition outside outer class's, while outer class contains instance of inner class

  4. 4

    Python - declare method name in dictionary with method definition in outside class

  5. 5

    Is method a class method or instance method

  6. 6

    Can I reference class attribute of sub-class in instance method?

  7. 7

    static_assert inside/outside class definition

  8. 8

    Variable assignment outside class definition

  9. 9

    Mocking a method outside of a class

  10. 10

    Move method definition for template nested class outside declaration

  11. 11

    Declare method outside of class

  12. 12

    Partial Template specialization definition outside of class definition

  13. 13

    Java8: Is there a way to get an instance method reference from a class method reference?

  14. 14

    friend not allowed outside of a class definition

  15. 15

    How to reference the instance of the outside class in Kotlin?

  16. 16

    TypeScript - How to add a method outside the class definition

  17. 17

    inner class method definition outside of a template class

  18. 18

    Method reference to instance method from class type vs Method reference to instance method from instance

  19. 19

    Beginner problem. Cannot access class instance methods outside main method?

  20. 20

    Python - declare method name in dictionary with method definition in outside class

  21. 21

    Is method a class method or instance method

  22. 22

    How to reference an instance method in another instance method of a different class?

  23. 23

    Dynamic method definition in a class

  24. 24

    regarding a method definition in a class

  25. 25

    Partial Template specialization definition outside of class definition

  26. 26

    Instance created before method definition

  27. 27

    TypeScript - How to add a method outside the class definition

  28. 28

    Java 8 method reference to class instance method NPE

  29. 29

    Move function outside class definition

HotTag

Archive