在ctypes结构中,是否可以指定默认值?
例如,使用常规python函数,您可以这样做:
def func(a, b=2):
print a + b
Run Code Online (Sandbox Code Playgroud)
这将允许这种行为:
func(1) # prints 3
func(1, 20) # prints 21
func(1, b=50) # prints 51
Run Code Online (Sandbox Code Playgroud)
是否可以在ctypes结构中执行此操作?
例如:
class Struct(Structure):
_fields_ = [("a", c_int), ("b", c_int)] # b default should be 2
def print_values(self):
print self.a, self.b
struct_instance = Struct(1)
struct_instance.print_values() # should somehow print 1, 2
Run Code Online (Sandbox Code Playgroud) 我有两个文件,都在同一个项目中(Web抓取框架的一部分).File1处理由File2生成的项目.在File2中,我有一个函数可以打印出有关进程的一些基本统计信息(生成了多少项的计数等).我在File1中有计数,我想用File1的统计数据打印但不确定如何做到这一点.看一下示例代码.
文件1:
class Class1(object):
def __init__(self):
self.stats = counter("name") #This is the instance that I'd like to use in File2
self.stats.count = 10
class counter:
def __init__(self, name):
self.name = name
self.count = 0
def __string__(self):
message = self.name + self.count
return message
Run Code Online (Sandbox Code Playgroud)
文件2 :(这是我想做的)
from project import file1 # this import returns no error
def stats():
print file1.Class1.stats # This is where I'm trying to get the instance created in Class1 of File2.
#print file1.Class1.stats.count # Furthermore, it would …Run Code Online (Sandbox Code Playgroud)