简体   繁体   English

根据操作系统,定义不同的方法

[英]Depending on the OS, define different methods

Although Python doesn't have the C pre-processor, I wonder, what's a way to declare methods and variables depending on the OS? 尽管Python没有C预处理器,但我想知道,有什么方法可以根据操作系统声明方法和变量? Something like: 就像是:

class C1:

#if defined(WINDOWS)
        self.var1 = 1

        def method1(....):
            pass

#elif defined(LINUX)
        self.var2 = 1

        def method2(....):
            pass

#endif

My question isn't how to detect the platform only . 我的问题不只是如何检测平台 But how to define different methods and variables depending on it also . 但是如何定义不同的方法和变量也要取决于

You can detect the current platform as follows 您可以按以下方式检测当前平台

import platform
platform.system()

The output for me is 对我来说输出是

'Linux'

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

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