简体   繁体   English

Python 面向对象在主 function 中创建 object

[英]Python object-oriented creating object in main function

I'm new to object oriented in Python, I'm a bit confused on how to work with object oriented.我是面向 Python 的 object 的新手,我对如何使用面向 object 的工作有点困惑。

class Beta(object):
    def __init__(self,message='defualt msg'):
        self.message = message
        
    def foo(self):
        return self.message
    def main():
        beta1 = Beta()
        messege = beta1.foo()

    
    if __name__ == '__main__':
        main()

The following code returns the following error NameError: name 'Beta' is not defined but the following code work:以下代码返回以下错误NameError: name 'Beta' is not defined但以下代码有效:

    from time import sleep

    def foo(data):
        print("Beginning data processing...")
        modified_data = data + " that has been modified"
        sleep(3)
        print("Data processing finished.")
        return modified_data

    def main():
        data = "My data read from the Web"
        print(data)
        modified_data = foo(data)
        print(modified_data)

    if __name__ == "__main__":
        main()

The only difference is the class can we create an object inside the main or call the functions from the main directly or we need to explicitly create an object outside the class.唯一的区别是 class 我们可以在 main 内部创建一个 object 还是直接从 main 调用函数,或者我们需要在 object 外部显式创建一个 object 。

In Java or C# we can use the class name in any class function to call for another functions, while in Python this is not working this is a script that generated the same error. In Java or C# we can use the class name in any class function to call for another functions, while in Python this is not working this is a script that generated the same error.

    class Test():
    def foo() :
        print('Hello ')

    def bar():
        foo()
        print('world')
    def run():
        test = Test()
        test.bar()

    run()

You defined your main function (and its call) inside the class itself...I think this is what you wanted...您在 class 本身内部定义了您的主要 function (及其调用)......我认为这就是你想要的......

class Beta(object):
    def __init__(self,message='defualt msg'):
        self.message = message
        
    def foo(self):
        return self.message

def main():
   beta1 = Beta()
   message = beta1.foo()

    
if __name__ == '__main__':
   main()

The correct indentation would be正确的缩进是

class Beta(object):
    def __init__(self,message='defualt msg'):
        self.message = message
        
    def foo(self):
        return self.message

def main():
    beta1 = Beta()
    messege = beta1.foo()


if __name__ == '__main__':
    main()

The function main uses Beta ; function main使用Beta it should not be part of Beta .它不应该Beta的一部分。 Likewise, the if statement ensures that main is only executed when the file is used as a script and not when it is imported, so is similarly not part of the class Beta .同样, if语句确保main仅在文件用作脚本时执行,而不是在导入时执行,因此同样不是 class Beta的一部分。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM