简体   繁体   English

如何在 class 中定义只能从 __init__ 方法调用的方法

[英]How to define method in class that can only be called from __init__ method

I have a simple Python class, with a constructor and a method.我有一个简单的 Python class,带有构造函数和方法。 I want the method to only be able to be called from the constructor, and not outside the class definition.我希望该方法只能从构造函数中调用,而不是在 class 定义之外。 Is there any way to do this in Python?在 Python 中有没有办法做到这一点? I know I can do this by defining a function inside the constructor, but I don't want to do that.我知道我可以通过在构造函数中定义一个 function 来做到这一点,但我不想这样做。

class Test:
    def __init__(self):
        self.do_something  # Should work

    def do_something(self):
        # do something

test = Test()
test.do_something()  # Should not work (Should not be a recognized method)

You need to preface do_something(self) with a double underscore.您需要在 do_something(self) 前面加上双下划线。 The code is below.代码如下。

class Test:
    def __init__(self):
        self.__do_something  # Should work

    def __do_something(self):
        # do something

test = Test()
test.__do_something()

Yes, you can mark methods with a double underscore prefix:是的,您可以使用双下划线前缀标记方法:

class Test:
    def __init__(self):
        self.__do_something()  # This works

    def __do_something(self):
        print('something')

test = Test()
test.__do_something()  # This does not work

Output: Output:

something
Traceback (most recent call last):

  File "something.py", line 11, in <module>
    test.__do_something()  # This does not work
AttributeError: 'Test' object has no attribute '__do_something'

To make it 'private' in python just prepend __ to its name.要使其在 python 中成为“私有”,只需在其名称前加上 __。 It won't be truly private though.不过,它不会是真正的私密。 It will just be by a slightly different name.只是名称略有不同。 You can still access it by running dir on an object from the class and once you know the name you can use it to call it outside the class.您仍然可以通过在 class 的 object 上运行 dir 来访问它,一旦您知道名称,您就可以使用它在 class 之外调用它。

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

相关问题 抽象类的错误“未调用基类的 __init__ 方法” - Error "__init__ method from base class is not called" for an abstract class Python:如何使用类方法在__init__函数中定义变量? - Python: How to define a variable in an __init__ function with a class method? 未调用基类&#39;*&#39;的Pylint错误__init__方法 - Pylint error __init__ method from base class '*' is not called 如何获取调用__init__方法的子类 - How to get the child class that called the __init__ method zope.interface可以定义类&#39;__init__方法应该如何看? - Can zope.interface define how a class' __init__ method should look? 我该如何定义只想在__init__方法中使用的Python方法,即无法从类定义外部访问的方法? - How do I define a Python method that I only want to use in the __init__ method, i.e., that is not accessible from outside the class definition? __init__是一种类方法吗? - Is __init__ a class method? 在哪里定义仅在__init__内部使用且不能在其他任何地方调用的方法? - Where to define a method that is used only inside __init__ and must not be called anywhere else? Python - 如何从__init__方法中引用类变量或方法? - Python - how can I reference a class variable or method from within the __init__ method? 如何访问 tkinter 类中 __init__() 方法中的元素? - How can I access the element in a __init__() method in a tkinter class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM