首页 > 解决方案 > 如何通过单击将变量作为参数传递给方法?

问题描述

您好,我对 click 和 python 比较陌生,并且正在尝试通过 click 将变量传递给类中的方法。我不确定点击装饰器是否应该高于init或高于方法本身。

import click
import os
from datetime import datetime


class table_update:
    scriptdirectory = os.path.dirname(os.path.realpath(__file__))
    @click.command()
    @click.option('-tn','--tablename',prompt=True)
    def __init__(self,*args,**kwargs):
        self.tablename = self.tablename
       
    # @click.group()
    # @click.command()
    # @click.option('--tn','--tablename',prompt=True)
    def gettime(self,tablename):
        """
        Set up timer and time functions for table update
        """
        print('get_time function running')
        print(f"Class variable ScriptDirectoryname inherited:{self.scriptdirectory}")
        print(f"here's the table: {tablename}")
        global_start = datetime.now()
        # get project path name & set to current path
        changedir= os.chdir(self.scriptdirectory)
        print(changedir)

time = table_update().gettime()
time

标签: python-3.xclassinheritancemethodsclick

解决方案


让我们从这个开始......

你需要写:

def __init__(self, tablename):
    self.tablename = tablename

将装饰器放在方法而不是构造器上......

老实说,我很难理解这段代码的目的是什么?你想让它做什么?如果您解释更多,我可以提供帮助:)


推荐阅读