Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional Class Creation (Python)

Tags:

python

From the tutorial: "A class definition is an executable statement."

Is the following recommended in a script?

my_switch = False
if my_switch:
    class Hello:
        def __init__(self):
            self.greeting = "Hello!"

else:
    class Hello:
        def __init__(self):
            self.greeting = "Salut!"
like image 392
atp Avatar asked Sep 16 '10 18:09

atp


1 Answers

You can even do

class Hello:
    def __init__(self):
        self.greeting = "Hello!"

class Salut:
    def __init__(self):
        self.greeting = "Salut!"

if my_switch:
    Hello = Salut

(note that your code needs lower-case Class keywords...)

like image 135
Radomir Dopieralski Avatar answered Oct 01 '22 07:10

Radomir Dopieralski