Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何使用return语句在python中返回类的变量?_Python 3.x_Time_Python 3.7 - Fatal编程技术网

Python 3.x 如何使用return语句在python中返回类的变量?

Python 3.x 如何使用return语句在python中返回类的变量?,python-3.x,time,python-3.7,Python 3.x,Time,Python 3.7,你知道如何实现吗?此外,如果该值可以作为整数返回,那就太好了。实际上,您离实现这一点还不远 您现在遇到的问题是,名称year作为类属性(此行:year=timelist[4])和方法名称(此行:def year(self):)之间存在冲突 您可以将代码更新为以下内容: > [<bound method curtime.year of <__main__.curtime object at > 0x00000285753E8470>>] 您将正确地获得以下输出

你知道如何实现吗?此外,如果该值可以作为整数返回,那就太好了。

实际上,您离实现这一点还不远

您现在遇到的问题是,名称
year
作为类属性(此行:
year=timelist[4]
)和方法名称(此行:
def year(self):
)之间存在冲突

您可以将代码更新为以下内容:

> [<bound method curtime.year of <__main__.curtime object at
> 0x00000285753E8470>>]
您将正确地获得以下输出:
['2019']


请注意,在这里,我删除了所有的类变量,并修复了
\uuuu init\uuuu
实现,这样每个实例都有自己的当前时间。关键是我使用
\u year
作为存储的私有值的属性名,并使用
year
作为要使用的函数的属性名。

\u int\uu()
?为什么要分配两次;一个内部函数,另一个外部函数?毕竟这里不需要类。这是因为我无法猜测这些变量将使用的数据类型。字符串类型对我不起作用。
> [<bound method curtime.year of <__main__.curtime object at
> 0x00000285753E8470>>]
import time


class curtime:

    def __init__(self):
        timeu = time.asctime(time.localtime(time.time())) 
        timelist = timeu.split()
        self._day = timelist[0]
        self._month = timelist[1]
        self._date = timelist[2]
        self._time = timelist[3]
        self._year = timelist[4]

    def year(self):
        return [self._year]


t1 = curtime()
years = t1.year()
print(years)