使用python中其他类的函数
我有很多类,其中函数几乎相同 假设函数x:使用python中其他类的函数,python,python-3.x,function,class,Python,Python 3.x,Function,Class,我有很多类,其中函数几乎相同 假设函数x: class A(): def x_A (self): ... ...do the same thing ... run a function that is unique in class A itself, say u_A ... ...do the same thing ... class B(): def x_B (
class A():
def x_A (self):
...
...do the same thing
...
run a function that is unique in class A itself, say u_A
...
...do the same thing
...
class B():
def x_B (self):
...
...do the same thing
..
run a function that is unique in class B itself, say u_B
...
...do the same thing
...
所以我想出了一个主意,在一个新类中重新编写函数x(比如C类中的x_C),以替换x_a和x_B。我只需要在需要时导入这个新类。比如:
import C
class A():
def x_A (self):
C.x_C(u_A)
class B():
def x_B (self):
C.x_C(u_A)
但是我对如何将惟一函数(u_A和u_B)作为变量传递并使python正确运行感到困惑
class C():
def x_C (self,unique_function):
...
...do the same thing
..
run unique_function here
...
...do the same thing
...
提前Thx
blow是新编辑的:
您好,我正在指定我的问题:
我有很多爬虫程序,在每一个爬虫程序的末尾,我都有“在插入之前运行”来检查它们是否可以正常运行。
目前,我只是复制和粘贴在每个完成的爬虫与一些编辑结束这个功能。
但是现在我想通过从其他文件导入“在插入之前运行”来简化我的代码,然后是我的问题
def run_before_insert(self):
try:
#store_list = []
comp_name = 'HangTen'
start = time.time()
print('{} runBeforeInsert START'.format(comp_name), '\n')
###Here is the part where small edits in the function:
store_list = self.get_stores_2()
###the rest is the same
script_info = {}
running_time = round(time.time() - start,2)
total = str(len(store_list))
script_info['running_time'] = running_time
script_info['total_stores'] = total
print('\n{} total stores : {}'.format(comp_name,script_info['total_stores']), '\n')
print('{} running time : {}'.format(comp_name,script_info['running_time']), '\n')
print('{} runBeforeInsert Done'.format(comp_name), '\n')
print('\n')
return script_info
except Exception as e:
traceback.print_exc()
script_info = {}
script_info['running_time'] = '--'
script_info['total_stores'] = 'error'
return script_info
print(e)
以下是我关于@juanpa.arrivillaga的代码:
class run_pkg_class():
def __init__(self):
pass
def run_before_insert(self, store_function, company_name):
try:
comp_name = company_name
start = time.time()
print('{} runBeforeInsert START'.format(comp_name), '\n')
###
store_list = store_function()
###
script_info = {}
running_time = round(time.time() - start,2)
total = str(len(store_list))
script_info['running_time'] = running_time
script_info['total_stores'] = total
print('\n{} total stores : {}'.format(comp_name,script_info['total_stores']), '\n')
print('{} running time : {}'.format(comp_name,script_info['running_time']), '\n')
print('{} runBeforeInsert Done'.format(comp_name), '\n')
print('\n')
return script_info
except Exception as e:
traceback.print_exc()
script_info = {}
script_info['running_time'] = '--'
script_info['total_stores'] = 'error'
return script_info
print(e)
并将以上内容导入hangten crawler类:
def run_before_insert2(self):
rp = run_pkg_class()
rp.run_before_insert(self.get_id())
在这种情况下,self.get\u stores\u 2()将返回一个列表
运行时发生“TypeError:“list”对象不可调用”
不确定原因,Python函数是一流对象。它们与任何其他属性一样。直接传给他们:
import C
class A:
def x_A (self):
C.x_C(self.u_A)
class B:
def x_B (self):
C.x_C(self.u_B)
在C
中,您可以这样称呼它:
unique_function()
考虑到
C
显然不关心A
和B
中的状态,我怀疑这些东西不应该是类的开始。如果我理解正确,您甚至不需要每次导入模块。相反,您可以创建一个基本类,其他类将从中继承函数。例如,类B和C从类A继承函数“power”
class A:
""" Example of class A """
def __init__(self):
self.num1 = num1
def power(self, num):
return num**3
class B (A):
""" Example of class B"""
def __init__(self, num1, num2):
super().__init__(num1)
self.num2 = num2
self.power_num1 = self.power(num1)
class C(A):
""" Example of class C"""
def __init__(self, num1, num2, num3):
super().__init__(num1)
self.num2 = num2
self.num3 = num3
def power_total(self):
print(self.power(self.num1) + self.power(self.num2)
+ self.power(self.num3))
使用示例:
>>> c = C(1, 2, 3)
>>> c.power_total()
36
>>> b = B(2, 4)
>>> b.power_num1
8
我之所以要创建一个类C,是因为将来可能会有其他类似的函数在类a和类B中运行。或者可能还有其他更好的做法?您可以使用不在类中且不与对象绑定的函数。@Pete是的,模块级函数在这里似乎非常合适。如果没有更具体的细节,很难给出更具体的建议。
import C
不导入类,而是导入一个模块(通常是由源文件C.py
定义的模块)。另外,如果C
是一个类而不是一个模块,并且x_C
是该类中的一个普通方法,那么你不能调用C.x_C
,你必须构造一个实例,C=C()
,然后你可以调用C.x_C
。同时,这可能更容易用具体的语言解释,有意义的名称,而不是像C
和x\u C
这样的名称。