简体   繁体   English

如果条件为真,如何运行 class?

[英]How to run a class if a condition is true?

I want to run the full class if a condition is true.如果条件为真,我想运行完整的 class。 I can do this by putting the full code inside a if block.我可以通过将完整的代码放在 if 块中来做到这一点。

is_active = True

class testClass():
    if is_active:
        print("Hello World")
        def func():
            print("this is a function")
        def func2():
            print("this is a another function")

But don't want to put the full code inside an if block.但不想将完整的代码放在if块中。 I want to do something like this, exiting for a condition.我想做这样的事情,退出一个条件。 What we do by return in functions.我们在函数中通过return做什么。

class testClass():
    if is_active is False:
        #Exit the class(like return in a function)
    print("Hello World")
    def func():
        print("this is a function")
    def func2():
        print("this is a another function")

My code looks something like this.我的代码看起来像这样。

active = True    
class class1(parent_class):    
    if active is False:
        # Exit the class(like return in a function)
    print("Hello World")
    def func(self):
        print("this is a function")
    def func2(self):
        print("this is a another function")

class class2(class1):
    # Inherited class1. 
    # class1 will only work if active is True

obj = class2()

Please help请帮忙

Instead of not declaring the methods you could delete them!您可以删除它们,而不是不声明方法!

active = False
class myclass:
    def myfunct1():
        print("I am function 1")
    def myfunct2():
        pass
    def myfunct3():
        print("I am function 3!")
    if not active:
        del(myfunct3)

myclass.myfunct1()
myclass.myfunct2()
myclass.myfunct3()

If you run this you'll get:如果你运行它,你会得到:

I am function 1
Traceback (most recent call last):
  File "test.py", line 14, in <module>
    myclass.myfunct3()
AttributeError: type object 'myclass' has no attribute 'myfunct3'

Change the first line to: active = True将第一行更改为: active = True

And you'll get:你会得到:

I am function 1
I am function 3!

Hope I helped!希望我有所帮助!

well, if you do not want to use if to not declare the functions or to delete them you can always add them!好吧,如果您不想使用 if 不声明函数或删除它们,您可以随时添加它们!

Example:例子:

def function1():
    print("I'm function 1")
print("[LOG] Declared function1 {}".format(function1))


if input("Active? [y/n]").lower() == "y":
    active = True
else:
    active = False

class MyClass:
    def __init__(self):
        if active:
            self.function1 = function1
        print("[LOG] Class initalized")
    def function2(self):
        print("I'm function 2")

print("[LOG] Declared MyClass {}".format(MyClass))

obj = MyClass()
obj.function2()
obj.function1()

Output (when yes): Output(如果是):

myuser@UbuntuVm:~/Desktop$ python test.py 
[LOG] Declared function1 <function function1 at 0x7f53cd4450d0>
Active? [y/n]y
[LOG] Declared MyClass <class '__main__.MyClass'>
[LOG] Class initalized
I'm function2
I'm function 1

Output (when no): Output(没有时):

myuser@UbuntuVm:~/Desktop$ python test.py 
[LOG] Declared function1 <function function1 at 0x7f9c54c090d0>
Active? [y/n]n
[LOG] Declared MyClass <class '__main__.MyClass'>
[LOG] Class initalized
I'm function 2
Traceback (most recent call last):
  File "test.py", line 23, in <module>
    obj.function1()
AttributeError: 'MyClass' object has no attribute 'function1'

I think that's what you meant, you could even change the functions when active is False.我认为这就是您的意思,您甚至可以在 active 为 False 时更改功能。

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

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