I have been playing around with the Click package and I can't get the following snippit of code to work properly.
import numpy as np
import click
@click.command()
@click.option('--get_print', default = True)
class CustomClass():
def __init__(s, get_print):
s.data = np.random.normal(0, 1, 10)
s.get_print = get_print
if s.get_print:
print("get_print =", s.get_print)
def print_mean(s):
print("Printing mean:")
mean_ = np.mean(s.data)
if s.get_print:
print(mean_)
if __name__ == '__main__':
CustomClass().print_mean()
It seems that the print_mean
function never gets called. If I comment out the @Click decorator, and manually set get_print=True
, the print_mean
method gets called just fine.
My suspicion is that @Click somehow sets __main__
and so when I do if __name__ == '__main__'
it just calls Click again - but perhaps I'm wrong?
In either case, what is the correct way of using Click, when you want the __init__
method to inherit all the options, and if __name__ == '__main__'
to actually be able to call functions inside the class, where the __init__
is defined?
@click.command()
; the only examples i found decorate functions... (but maybe i missed something in the doc...) – Rutaceous__init__
can inherit commands from click, without "taking over" the use of__main__
. How else would you use Click together with classes? – Headbandclick
interact directly with your class and not via a function as in all the examples? – Rutaceous