- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我的目标是减少我创建的类中的一些冗余。我已将问题简化为一个非常简单的示例
class BigNumbers(object):
def __init__(self, big_number):
self.number = big_number
@classmethod
def make_with_sums(cls, sum_to):
fin_num = 0
for i in range(1, sum_to):
fin_num += i
return cls( fin_num )
@classmethod
def make_with_product(cls, mul_to):
fin_num = 1
for i in range(1, mul_to):
fin_num *= i
return cls( fin_num )
class dnryNumbers(object):
def __init__(self, big_number):
self.number = big_number
@classmethod
def _make_with_helper(cls, make_meth, fin_num):
method_dict = {'sum': lambda x, y: x + y,
'prod': lambda x, y: x * y
}
var = 0
for i in range(1, fin_num):
var = method_dict[make_meth](var, i)
return cls( var )
def make_with_sums(self, sum_to):
return self._make_with_helper( 'sum', sum_to )
def make_with_product(self, mul_to):
return self._make_with_helper('prod', mul_to )
我的目标是使用与使用 BigNumbers
类时相同的函数调用,例如:
In [60]: bn = BigNumbers.make_with_product(10)
In [61]: bn.number
Out[61]: 362880
-- 或 --
In [63]: bn = BigNumbers.make_with_sums(10)
In [64]: bn.number
Out[64]: 45
但是当前的功能不起作用:
In [65]: bn = dnryNumbers.make_with_product(10)
TypeError: unbound method make_with_product() must be called with dnryNumbers instance as first argument (got int instance instead)
最佳答案
简单的答案:make_with_sums
和make_with_products
是类方法,而不是实例方法,因此需要这样声明。另请注意,_make_with_helper
还需要采用起始 值作为参数;将 var
初始化为 0 将使 make_with_product
返回 cls(0)
,无论其参数是什么。
_make_with_helper
,
method_dict
都是相同的字典,因此它应该是一个类变量:
class dnryNumbers(object):
def __init__(self, big_number):
self.number = big_number
method_dict = {'sum': lambda x, y: x + y,
'prod': lambda x, y: x * y
}
@classmethod
def _make_with_helper(cls, make_meth, fin_num, starting_value):
var = starting_value
for i in range(1, fin_num):
var = dnryNumbers.method_dict[make_meth](var, i)
return cls( var )
@classmethod
def make_with_sums(cls, sum_to):
return cls._make_with_helper('sum', sum_to, 0)
@classmethod
def make_with_product(cls, mul_to):
return cls._make_with_helper('prod', mul_to, 1)
但是现在,method_dict
只是添加了一个您不需要的额外间接层。由于这些函数不打算在类外部使用,因此只需将它们定义为“私有(private)”方法,并直接使用对它们的引用。
class dnryNumbers(object):
def __init__(self, big_number):
self.number = big_number
@staticmethod
def _sum(x, y):
return x + y
@staticmethod
def _prod(x, y):
return x * y
@classmethod
def _make_with_helper(cls, make_meth, fin_num, starting_value):
var = starting_value
for i in range(1, fin_num):
var = make_meth(var, i)
return cls(var)
@classmethod def make_with_sums(cls, sum_to): 返回 cls._make_with_helper(dnryNumbers._sum, sum_to, 0)
@classmethod
def make_with_product(cls, mul_to):
return cls._make_with_helper(dnryNumbers._prod, mul_to, 1)
最后,值得指出的是,除非您的实际代码比此处显示的示例更复杂,否则 _sum
已经可以用作 operator.add
、_prod
只是 operator.mul
,而 _make_with_helper
只是 reduce
内置函数(或 functools.reduce
,如果是 Python 3)。
import operator
try:
reduce
except NameError:
from functools import reduce
class dnryNumbers(object):
def __init__(self, big_number):
self.number = big_number
@classmethod
def _make_with_helper(cls, make_meth, fin_num, starting_value):
return cls(reduce(make_meth, range(1, fin_num), starting_value))
@classmethod
def make_with_sums(cls, sum_to):
return cls._make_with_helper(operator.add, sum_to, 0)
@classmethod
def make_with_product(cls, mul_to):
return cls._make_with_helper(operator.add, mul_to, 1)
关于python - python 类中的类方法和辅助函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26222710/
我正在尝试 specFlow 辅助,但不确定如何从表中创建类属性。 想象一下我有这门课: public class Tracking { public string Category { ge
我如何使用带 IOS 应用程序的辅助 GPS 来计算给定区域(例如建筑物)内部(或外部)某人的位置? 是否有可能在几英尺内就足够准确? 这样做正确吗? 是否可以在计算中使用多个 wifi 连接? 最佳
我在 wiki 和其他一些文本中看到,他们说冒泡排序、插入排序、选择排序等的空间复杂度是 O(1) 辅助。它们是否指的是程序中使用的变量所需的常量存储单元。 最佳答案 是的,他们指的是大多数排序都是就
默认情况下,页面上有 3 个点击事件(蓝色 X、蓝色 +、灰色 X)。每个人都会打开一个模式框。 每个模式框都有一个按钮。其中两个模态框,一个用于蓝色 X,一个用于蓝色 +,内部都有功能按钮。当我单击
我正在寻找 Kotlin 的 gigasecond 练习的解决方案:http://exercism.io/exercises/kotlin/gigasecond/readme 。我可以理解它如何需要两
我基本上刚刚开始使用 PyGame 进行开发,但我在整个 Sprite 概念方面遇到了麻烦。我一直在到处寻找有关如何使用它的指南,但似乎找不到任何指南。我想知道这一切是如何运作的基本概念。这是我一直在
我有一些无法运行的 JavaScript 代码。我尝试过移动一些东西,并更改一些关键字,但到目前为止没有任何效果。我会让你们尝试一下。 这是 JavaScript 文件: var GAME =
我有这个注册网页是我在帮助下创建的,感谢这里的人。在尝试使其响应之前,我只是做了一些调整。如何在复选框及其文本和底部的 div 之间创建空间而没有间隙。有什么建议吗? https://jsfiddle
我正在尝试检查是否启用了 WiFi 辅助。当我连接到我的接入点以获取一些数据时,我遇到了问题,当我的连接不佳时,我的蜂窝数据被使用并且它干扰了我的接入点。有什么方法可以检查是否启用了此选项? 最佳答案
为了安全起见,我希望使用异地复制/辅助 Blob 存储容器作为 AzureML 数据存储的数据源。所以我执行以下操作: 新数据存储 输入名称 + Azure Blob 存储 + 手动输入 对于 URL
我的讲师现在有一个我以前从未见过的奇怪习惯,我想知道这是 Haskell 标准还是他的编程风格的怪癖。 基本上,他经常会做这样的事情: functionEx :: String -> Int func
我想从可移动SD卡中删除文件,我尝试了很多方法但没有效果。 尝试过: file.delete(); 和 File file = new File(selectedFilePath); boolean
我正在开发一款 Android 应用,用户必须能够在其中进行身份验证,然后调用 YouTube 数据 API。 我可以毫无问题地使用主帐户对用户进行身份验证,使用 Google 登录对我和用户来说一切
命令: sudo mv /temp/hello.txt /path/to/destination/ 然后我通过 key 存储添加了密码。 我确信写在 sudo.password 中的密码是正确的。
我需要编写一个 java 代码来获取给定集群的辅助名称节点的 IP 地址。给定集群的 Namenode 的 IP 地址。 我能够获取数据节点和名称节点的报告,但无法找到获取辅助名称节点的 IP 地址的
Cay Horstmann 的书《不耐烦的 Scala》中的一个非常简单的练习一直让我感到困惑。是关于primary , auxiliary和 default primary构造函数: ex 5.10
我正在尝试确定 Google Cloud DNS 是否支持通过 NOTIFY 请求进行辅助 DNS (AXFR/IXFR) 传输?我在网上找不到任何东西,Google 也没有明确声明不支持它。 最佳答
我有一个简单的 Kotlin 类: data class ValveSpan(val begin:Duration, val end:Duration, val key:String):Compara
我有一个与最初在 UISplitView 中加载辅助 View 相关的快速问题。目前,我已经在 masterVC.swift 中获得了代码,可以用数组中的第一个对象(如果有)填充detailsVC。这
我正在使用这个命令来获取另一个命令的进程 ID: ps aux | grep 7000.conf | awk '{print $2}' 这将返回两个 PID: 7731 22125 我只想要第一个。第
我是一名优秀的程序员,十分优秀!