在 class python 中定义函数的正确方法
proper way to define functions inside class python
我是 python 的新手,我有一些 python 与 dll 库(C 语言)交互的代码。
我的 python 代码现在运行良好,但我被告知要将一些初始化实现到它们自己的函数中。比如我现在的是:
hello.py
import ctypes
class my_outer_class:
def __init__(self):
test = ctypes.WinDLL('C:\Users\OneDrive\HelloWorld\HelloWorld\loc\Debug\HelloWorld.dll')
self.py_function_1 = test.function_1
self.py_function_1.argtype = (ctypes.c_uint8,ctypes.c_uint8 )
self.py_function_1.restype = ctypes.c_int
self.py_function_2 = test.function_2
self.py_function_2.argtype = (ctypes.c_uint8,ctypes.c_uint8 )
self.py_function_2.restype = ctypes.c_int
run_test.py
import hello
import ctypes
myapi = hello.my_outer_class()
result = myapi.py_function_1(123,123)
print(result)
result = myapi.py_function_2(123,123)
print(result)
我想知道是否可以将我的 hello.py 更改为:
hello.py
import ctypes
class my_outer_class:
def __init__(self):
test = ctypes.WinDLL('C:\Users\OneDrive\HelloWorld\HelloWorld\loc\Debug\HelloWorld.dll')
def func_1(self):
self.py_function_1 = test.function_1
self.py_function_1.argtype = (ctypes.c_uint8,ctypes.c_uint8 )
self.py_function_1.restype = ctypes.c_int
def func_2(self):
self.py_function_2 = test.function_2
self.py_function_2.argtype = (ctypes.c_uint8,ctypes.c_uint8 )
self.py_function_2.restype = ctypes.c_int
run_test.py
import hello
import ctypes
myapi = hello.my_outer_class()
result = myapi.func_1(123,123)
print(result)
result = myapi.func_2(123,123)
print(result)
当我 运行 修改后的版本时出现错误:
Traceback (most recent call last):
File "C:\Users\OneDrive\run_test.py", line 6, in <module>
result = myapi.func_1(123,123)
AttributeError: 'my_outer_class' object has no attribute 'func_1'
>>>
感谢任何建议,谢谢。
修改hello.py为
import ctypes
class my_outer_class:
def __init__(self):
self.test = ctypes.WinDLL('C:\Users\giova\OneDrive\Escritorio\HelloWorld\HelloWorld\loc\Debug\HelloWorld.dll')
def func_1(self, var1, var2):
self.py_function_1 = self.test.function_1
self.py_function_1.argtype = (ctypes.c_uint8,ctypes.c_uint8 )
self.py_function_1.restype = ctypes.c_int
def func_2(self, var1, var2):
self.py_function_2 = self.test.function_2
self.py_function_2.argtype = (ctypes.c_uint8,ctypes.c_uint8 )
self.py_function_2.restype = ctypes.c_int
和run_test.py
import hello
import ctypes
myapi = hello.my_outer_class()
result = myapi.func_1(123,123)
print(result)
result = myapi.func_2(123,123)
print(result)
此时我没有收到任何错误,我得到了这个输出
None
None
>>>
而不是预期值 1 和 0。我能够在我的代码的第一个版本中获得这些值。 “self”旁边的其他两个参数是否也需要与我在 argtype 下的参数相匹配?例如
def func_1(self, ctypes.c_uint8, ctypes.c_uint8):
因为我尝试过这种方式,它给了我一个无效的语法错误。
def func_1(self, ctypes.c_uint8, ctypes.c_uint8):
^
SyntaxError: invalid syntax
本质上,您在这里遇到了缩进问题。
请重新缩进您的 class 中的 func x 以使它们可以从外部访问。这是一个简单的例子:
class my_outer_class:
def __init__(self):
print("initiated")
def func_1(self):
print("Hello FUNC_1")
def func_2(self):
print("Hello FUNC_2")
myapi = my_outer_class()
myapi.func_1()
myapi.func_2()
要更深入地了解 classes 和 Python 中的面向对象编程,您可以从以下内容开始:https://python-textbok.readthedocs.io/en/1.0/Classes.html
我是 python 的新手,我有一些 python 与 dll 库(C 语言)交互的代码。 我的 python 代码现在运行良好,但我被告知要将一些初始化实现到它们自己的函数中。比如我现在的是:
hello.py
import ctypes
class my_outer_class:
def __init__(self):
test = ctypes.WinDLL('C:\Users\OneDrive\HelloWorld\HelloWorld\loc\Debug\HelloWorld.dll')
self.py_function_1 = test.function_1
self.py_function_1.argtype = (ctypes.c_uint8,ctypes.c_uint8 )
self.py_function_1.restype = ctypes.c_int
self.py_function_2 = test.function_2
self.py_function_2.argtype = (ctypes.c_uint8,ctypes.c_uint8 )
self.py_function_2.restype = ctypes.c_int
run_test.py
import hello
import ctypes
myapi = hello.my_outer_class()
result = myapi.py_function_1(123,123)
print(result)
result = myapi.py_function_2(123,123)
print(result)
我想知道是否可以将我的 hello.py 更改为:
hello.py
import ctypes
class my_outer_class:
def __init__(self):
test = ctypes.WinDLL('C:\Users\OneDrive\HelloWorld\HelloWorld\loc\Debug\HelloWorld.dll')
def func_1(self):
self.py_function_1 = test.function_1
self.py_function_1.argtype = (ctypes.c_uint8,ctypes.c_uint8 )
self.py_function_1.restype = ctypes.c_int
def func_2(self):
self.py_function_2 = test.function_2
self.py_function_2.argtype = (ctypes.c_uint8,ctypes.c_uint8 )
self.py_function_2.restype = ctypes.c_int
run_test.py
import hello
import ctypes
myapi = hello.my_outer_class()
result = myapi.func_1(123,123)
print(result)
result = myapi.func_2(123,123)
print(result)
当我 运行 修改后的版本时出现错误:
Traceback (most recent call last):
File "C:\Users\OneDrive\run_test.py", line 6, in <module>
result = myapi.func_1(123,123)
AttributeError: 'my_outer_class' object has no attribute 'func_1'
>>>
感谢任何建议,谢谢。
修改hello.py为
import ctypes
class my_outer_class:
def __init__(self):
self.test = ctypes.WinDLL('C:\Users\giova\OneDrive\Escritorio\HelloWorld\HelloWorld\loc\Debug\HelloWorld.dll')
def func_1(self, var1, var2):
self.py_function_1 = self.test.function_1
self.py_function_1.argtype = (ctypes.c_uint8,ctypes.c_uint8 )
self.py_function_1.restype = ctypes.c_int
def func_2(self, var1, var2):
self.py_function_2 = self.test.function_2
self.py_function_2.argtype = (ctypes.c_uint8,ctypes.c_uint8 )
self.py_function_2.restype = ctypes.c_int
和run_test.py
import hello
import ctypes
myapi = hello.my_outer_class()
result = myapi.func_1(123,123)
print(result)
result = myapi.func_2(123,123)
print(result)
此时我没有收到任何错误,我得到了这个输出
None
None
>>>
而不是预期值 1 和 0。我能够在我的代码的第一个版本中获得这些值。 “self”旁边的其他两个参数是否也需要与我在 argtype 下的参数相匹配?例如
def func_1(self, ctypes.c_uint8, ctypes.c_uint8):
因为我尝试过这种方式,它给了我一个无效的语法错误。
def func_1(self, ctypes.c_uint8, ctypes.c_uint8):
^
SyntaxError: invalid syntax
本质上,您在这里遇到了缩进问题。
请重新缩进您的 class 中的 func x 以使它们可以从外部访问。这是一个简单的例子:
class my_outer_class:
def __init__(self):
print("initiated")
def func_1(self):
print("Hello FUNC_1")
def func_2(self):
print("Hello FUNC_2")
myapi = my_outer_class()
myapi.func_1()
myapi.func_2()
要更深入地了解 classes 和 Python 中的面向对象编程,您可以从以下内容开始:https://python-textbok.readthedocs.io/en/1.0/Classes.html