当前位置:首页 » 编程语言 » python调用class

python调用class

发布时间: 2023-07-03 23:20:23

‘壹’ python怎么在一个类中调用另一个类的方法

class
a()
def
bf(self,event)
name
=
'bf'
#
这里的name是局部变量,不是class.a的变量,
当a.bf(event)执行完后就不存在了。
如果希望保持该变量的值在class.a中,:
class
a(object):
name
=
''
def
bf(self,
event):
a.name
=
'bf'
class
b(object):
def
exe(self,
event):
a.bf(event)
print
a.name

‘贰’ 对python 中class与变量的使用方法详解

python中的变量定义是很灵活的,很容易搞混淆,特别是对于class的变量的定义,如何定义使用类里的变量是我们维护代码和保证代码稳定性的关键。

枚举了各种情况,没有办法全部枚举,但大部分情况应该都已经包含了。

1. 类变量:能够通过类名或者object的self来访问到,在类的内部和外部均可达,比如class_var_1

2. 对象变量:可以通过对象的self来使用的变量,通过constructor一路走向去的的self初次被赋值的变量都会成为对象变量,比如object_var_1, object_var_2, object_var_3, object_var_4

3. 内部变量:可以在函数中定义,并加上self前缀,在初次调用过定义的函数后,就可以在后面的对象的函数中被使用,比如internal_var_1

4. 局部变量:在函数内部定义,并使用的变量,在使用完之后就会被回收对类及object不可见

5. 全局变量:定义在类或者函数外部,作用域在变量被定义之后的任意代码段,比如:global_var_1

‘叁’ python里面怎么调用class

以下代码调试通过:

classLuciaClass:#定义类
defluciaprint(self,text):#类里面的方法
print(' ',text)#方法就是输出text


x=LuciaClass()#方法的实例x
x.luciaprint('todayisabadday~~~')#实例调用类方法

运行效果:

‘肆’ python 如何调用类的方法

以numpy为例,首先创建一个对象:

In[32]:a=numpp.arange(10)

然后,a就拥有了类的方法,例如求和:

In[33]:a.sum()
Out[33]:45

也可以使用类的方法,将其应用到类的对象上,例如:

In[34]:numpp.sum(a)
Out[34]:45

‘伍’ python如何调用C++外部库中的类

你好流程比较复杂,需要通过一个c的接口来做。下面是一个简单的例子,你也可以到booth去看看他们是怎么用的。
//YourFile.cpp (compiled into a .dll or .so file)
#include
//For std::nothrow
//Either include a header defining your class, or define it here.
extern "C" //Tells the compile to use C-linkage for the next scope.
{
//Note: The interface this linkage region needs to use C only.
void * CreateInstanceOfClass( void )
{
// Note: Inside the function body, I can use C++.
return new(std::nothrow) MyClass;
}
//Thanks Chris.
void DeleteInstanceOfClass (void *ptr)
{
delete(std::nothrow) ptr;
}
int CallMemberTest(void *ptr)
{
// Note: A downside here is the lack of type safety.
// You could always internally(in the C++ library) save a reference to all
// pointers created of type MyClass and verify it is an element in that
//structure.
//
// Per comments with Andre, we should avoid throwing exceptions.
try
{
MyClass * ref = reinterpret_cast
(ptr);
return ref->Test();
}
catch(...)
{
return -1; //assuming -1 is an error condition.
}
}
} //End C linkage scope.

第二步:
gcc -shared -o test.so test.cpp
#creates test.so in your current working directory.
第三步:
>>> from ctypes import cdll
>>> stdc=cdll.LoadLibrary("libc.so.6") # or similar to load c library
>>> stdcpp=cdll.LoadLibrary("libstdc++.so.6") # or similar to load c++ library
>>> myLib=cdll.LoadLibrary("/path/to/test.so")
>>> spam = myLib.CreateInstanceOfClass()
>>> spam
[outputs the pointer address of the element]
>>> value=CallMemberTest(spam)
[does whatever Test does to the spam reference of the object]

热点内容
命令行访问ftp 发布:2025-02-09 14:10:53 浏览:61
加密文件模板 发布:2025-02-09 14:10:13 浏览:223
翁虹ftp 发布:2025-02-09 14:02:54 浏览:131
java加密对称 发布:2025-02-09 13:55:49 浏览:412
坤诩钱包为什么没有安卓版 发布:2025-02-09 13:50:49 浏览:299
存储过程性能优化 发布:2025-02-09 13:42:59 浏览:729
源码失窃 发布:2025-02-09 13:38:34 浏览:527
自动浏览器脚本 发布:2025-02-09 13:37:00 浏览:141
易语言问道源码 发布:2025-02-09 12:59:03 浏览:664
ip和服务器有关吗 发布:2025-02-09 12:51:26 浏览:952