![](/img/trans.png)
[英]Is it possible to access a variable from a class's __init__ by a function from the same class?
[英]Is it possible to access work function's variable in __init__ of a GNU Radio block?
在下面的GNU Radio處理塊中,首先我不知道是誰/什么將input_items
的值input_items
給work
函數。 是否可以將該值傳遞給__init__
函數?
我有一個文件xyz.py:
class xyz(gr.sync_block):
"""
docstring for block add_python
"""
def __init__(self, parent, title, order):
gr.sync_block.__init__(self,
name="xyz",
in_sig=[numpy.float32,numpy.float32],
out_sig=None)
................
................
//I want to access the value of input_items here
...............
...............
def work(self, input_items, output_items):
................
__init__
函數僅被調用一次以“初始化”該類的新實例。 除了設置輸入和輸出類型以使模塊可以成功連接之外,它與在流程圖中移動數據無關。
因此,在top_block
,您可能具有:
proc = xyz() # xyz's __init__ is called here
self.connect(source, proc, sink) # still no input_items, just connected flowgraph
然后,運行流程圖:
tb = top_block()
tb.run() # 'input_items' are now passed to 'work' of each block in succession
運行流程圖時,GNU Radio調度程序從源代碼塊中獲取大量樣本,並將其放入緩沖區中。 然后,將該緩沖區以及用於輸出項目的“空”緩沖區傳遞給流程圖中下一個塊的work
函數。
因此,當有數據要處理時,調度程序會自動調用每個塊的work
功能。 __init__
不能訪問任何要work
的參數,因為在調用__init__
時,甚至沒有將input_items
傳遞給work
。
work
是與__init__
完全分開的功能。 在該函數之外無法訪問其參數。 如果要訪問input_items
,請將其添加到__init__
參數列表中,並在調用__init__
時將其傳遞。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.