我試圖將一些診斷添加到嵌套的ctypes結構,但未能如此,並想知道原因。什麼作品如預期的那樣裸機例如:無法將屬性添加到ctypes中的嵌套結構
import ctypes
class FirstStruct(ctypes.Structure):
_fields_ = [('ch', ctypes.c_ubyte)]
f = FirstStruct()
print type(f)
print hasattr(f, 'helper')
f.helper = 'xyz'
print hasattr(f, 'helper')
這些行打印我的預期:
<class '__main__.FirstStruct'>
False
True
但是,當我用這個作爲另一種結構失敗:
class SecondStruct(ctypes.Structure):
_fields_ = [('first', FirstStruct)]
s = SecondStruct()
print type(s.first)
print hasattr(s.first, 'helper')
s.first.helper = 'xyz'
print hasattr(s.first, 'helper')
以上結果爲
<class '__main__.FirstStruct'>
False
False
有人可以解釋我的區別? (我跑它的Python 2.7.8你要知道,我不想改變結構本身,而是想添加ctypes的結構之外的額外的變量。)
編輯:
這裏有一個更直接的例子:
import ctypes
class FirstStruct(ctypes.Structure):
_fields_ = [('ch', ctypes.c_ubyte)]
class SecondStruct(ctypes.Structure):
_fields_ = [('first', FirstStruct)]
f = FirstStruct()
s = SecondStruct()
f.helper = 'aaa'
s.first.helper = 'bbb'
s.first.ch = 0
t = s.first
t.helper = 'ccc'
t.ch = 12
print f.helper # aaa
print t.ch # 12
print s.first.ch # 12
print t.helper # ccc
print s.first.helper # AttributeError: 'FirstStruct' object has no attribute 'helper'
的問題是:爲什麼不是s.first
和t
等價的,如果我CA,爲什麼不呢s.first.helper
觸發警告畢竟沒有設置它?
簡短的答案是ctype.Structures不可變。可變對象的賦值指向與源相同的內存位置。使用結構,即使保存類型(FirstStruct),也會創建一個新值。這意味着改變源,不會改變分配的變量。字典可能會爲你做詭計。你有沒有嘗試過使用字典作爲最高級別的容器? –
@RonNorris對不起,我幾乎明白你寫了什麼......但還沒有。嘗試使用https://stackoverflow.com/a/4828831/501814回答一個不可變對象的概念,但是在嘗試分配一個新屬性時,這裏出現屬性錯誤(正如我期望從s.first中得到的那樣if它是真正不可變的。) 是不可變的s.first的ta可變版本,指向相同的ctypes內存位置,但來自具有'helper'屬性的不同位置,而s.first的更改版本會立即丟失,因爲s的不變性? – Andris
非常。 's.helper ='sss''會像't.helper ='ccc'一樣工作,因爲你不會搞亂原始的FirstStruct對象 - 你實際上是在給's'添加一個屬性。 –