如何通过单击将变量作为参数传递给方法?

问题描述 投票:0回答:1

我试图通过单击将变量传递给类中的方法。我不确定单击装饰器是否应该位于 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.x class inheritance methods python-click
1个回答
0
投票

让我们从这个开始...

您需要写:

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.