當前位置:首頁 » 編程語言 » 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]

熱點內容
非加密公章 發布:2025-02-10 09:09:52 瀏覽:620
京東登錄密碼如何清除 發布:2025-02-10 09:07:41 瀏覽:689
dns伺服器地址192 發布:2025-02-10 09:07:39 瀏覽:661
redis緩存實現 發布:2025-02-10 09:01:26 瀏覽:710
後台登錄腳本 發布:2025-02-10 08:56:11 瀏覽:658
我的辣雞賬號和密碼是多少 發布:2025-02-10 08:55:37 瀏覽:690
超父演算法 發布:2025-02-10 08:43:05 瀏覽:910
電腦主機配置需要哪些硬體 發布:2025-02-10 08:22:52 瀏覽:706
平板太卡換存儲卡有用嗎 發布:2025-02-10 08:14:16 瀏覽:828
台北伺服器搭建 發布:2025-02-10 08:13:33 瀏覽:273