[Python] Calling a class method using function pointers in Python [duplicate]

Discussão em 'Python' iniciado por Stack, Junho 5, 2025.

  1. Stack

    Stack Membro Participativo

    Consider the following python code

    global_list = []
    class class1:
    def __init__(self):
    global_list.insert(0,self.method1)
    self.label = "class 1"

    def method1(self):
    print(self.label)

    class class2:
    def __init__(self):
    self.label = "class 2"

    def method2(self):
    global_list[0]()

    ob1 = class1()
    ob2 = class2()
    ob2.method2()


    When ran, the code, as I somehow expect, prints "class 1". But I do not know how it "remembers" the context and the implicit "self" argument ... Can somebody explain?

    Continue reading...

Compartilhe esta Página