gpt4 book ai didi

python - python 类中的类方法和辅助函数

转载 作者:太空宇宙 更新时间:2023-11-03 18:09:44 30 4
gpt4 key购买 nike

我的目标是减少我创建的类中的一些冗余。我已将问题简化为一个非常简单的示例

我当前的类(class)

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) (DNRY)

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_sumsmake_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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com