简体   繁体   English

类方法不能调用同一类中的其他类方法

[英]Class method can not call other class methods within the same class

I have a class defined with two methods: 我有一个用两种方法定义的类:

class A:
    def called():
        print 'called'
    def caller(self):
        called()

But caller can not use called directly 但是来电者不能直接使用被叫

A().caller()

gives error 给出错误

NameError: global name 'called' is not defined

How can I call the other unbounded method within the same class ? 如何在同一类中调用其他无界方法?

Qualify the method with self or the class name A . self或类名A限定方法。

class A:

    @staticmethod
    def called():
        print 'called'

    def caller(self):
        self.called()
        # Or
        A.called()

NOTE I changed the method called as a static method. 注意我更改了called静态方法的方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 快捷方式和/或避免在同一类的其他方法中对方法的“自身”引用 - Shortcut to and/or avoiding the “self” reference to a method within other methods of the same class 使用装饰器函数,它是python中类中其他方法的类中的方法 - Using a decorator function that is a method in a class on other methods within the class in python 在同一类中的方法中调用变量 - call variable in method within the same class Python:当从另一个类中调用初始方法时,类方法调用其他类方法 - Python: class methods calling other class methods when initial method is called from within another class 在 class 内部,我可以在另一个方法中调用其他方法变量吗? - Inside class, can i call other methods variable inside another method? 装饰 class 方法以调用同名方法 - Decorating class methods to call a method from the same name 调用类实例(类中的函数)上的方法,该方法调用该类中的其他函数 - Call a method on a class instance (function in class) that calls other functions within that class 无法在其他类别中呼叫方法 - Cannot call method in other class 如何存储/缓存类中的方法的值以供以后在同一类的其他方法中使用? - How can I store / cache values from methods in a class for later use in other methods of the same class? PYTHON:能够从同一类中调用方法有什么必要? - PYTHON: What is necessary to be able to call a method from within the same class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM