Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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 如何延伸箭头?(和模块中的类似类)_Python_Class_Python 3.x_Python Module_Arrow Python - Fatal编程技术网

Python 如何延伸箭头?(和模块中的类似类)

Python 如何延伸箭头?(和模块中的类似类),python,class,python-3.x,python-module,arrow-python,Python,Class,Python 3.x,Python Module,Arrow Python,我正在尝试扩展,但无法理解如何复制基类的功能。这可能是因为对如何在模块中扩展类缺乏清晰的理解(我的arrow案例强调了这一点,也就是说,这个问题可能比仅限于arrow更一般) 箭头基本用法: >>> import arrow >>> arrow.now() <Arrow [2016-11-19T15:13:23.897484+01:00]> >>> arrow.get("2016-11-20") <Arrow [2016-1

我正在尝试扩展,但无法理解如何复制基类的功能。这可能是因为对如何在模块中扩展类缺乏清晰的理解(我的
arrow
案例强调了这一点,也就是说,这个问题可能比仅限于
arrow
更一般)

箭头
基本用法:

>>> import arrow
>>> arrow.now()
<Arrow [2016-11-19T15:13:23.897484+01:00]>
>>> arrow.get("2016-11-20")
<Arrow [2016-11-20T00:00:00+00:00]>
结果是

tomorrow
Traceback (most recent call last):
  File "D:/Dropbox/dev/domotique/testing/myarrow.py", line 23, in <module>
    someday = MyArrow.get("2016-11-19")
AttributeError: type object 'MyArrow' has no attribute 'get'
明天
回溯(最近一次呼叫最后一次):
文件“D:/Dropbox/dev/domotique/testing/myarrow.py”,第23行,在
有一天=我的箭头。获取(“2016-11-19”)
AttributeError:类型对象“MyArrow”没有属性“get”
因此,第一部分工作正常,但
get()
失败。我看了一下,发现
get
ArrowFactory
中。如果我扩展
ArrowFactory
而不是
Arrow
,我将能够使用
get
,但不再使用
now()

这就是我所困惑的地方:上面的“基本用法”表明我可以调用
箭头。无论可用的是什么
,无论它是在类
箭头中定义的
还是
箭头工厂

这是如何工作的?
如何添加我的
when
方法以保持
箭头的其余部分(及其所有方法)不变

  • 可扩展为您自己的箭头派生类型
是Arrow文档中突出显示的问题之一,它实际演示了:

工厂 使用工厂将Arrow的模块API用于自定义Arrow 类型。首先,派生您的类型:

>>> class CustomArrow(arrow.Arrow):
...
...     def days_till_xmas(self):
...
...         xmas = arrow.Arrow(self.year, 12, 25)
...
...         if self > xmas:
...             xmas = xmas.replace(years=1)
...
...         return (xmas - self).days
然后获取并使用一个工厂:

>>> factory = arrow.Factory(CustomArrow)
>>> custom = factory.utcnow()
>>> custom
>>> <CustomArrow [2013-05-27T23:35:35.533160+00:00]>

>>> custom.days_till_xmas()
>>> 211
>>工厂=箭头。工厂(自定义箭头)
>>>custom=factory.utcnow()
>>>习俗
>>> 
>>>custom.days_至_圣诞节()
>>> 211
然后,您可以在
工厂
上调用
.get
.now
.utcnow
方法,并在
方法时获取自定义子类及其


这是专门处理Arrow及其模块级API的;对于更简单的模块,您可以直接对它们的类进行子类化。

arrow.get
不是
arrow
上的方法,而是公开工厂API的方法。通过调用
ArrowFactory(MyArrow)
,您可以为自己的类创建一个新工厂,然后可以调用
.get
。该具体问题与
箭头相关,内部相对复杂;一般来说,对模块的类进行子类化就足够了。谢谢。该死的,为什么我从来没有在文档中看到过这些东西?给出的例子几乎与我想要实现的目标完全相同(是的,我在查看源代码时非常害怕)
>>> factory = arrow.Factory(CustomArrow)
>>> custom = factory.utcnow()
>>> custom
>>> <CustomArrow [2013-05-27T23:35:35.533160+00:00]>

>>> custom.days_till_xmas()
>>> 211