本文介绍了麻烦参照理解传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我真的感到很困惑通过在C#参考了解通。在我的代码我有函数,有两个参数 私人布尔SerialUnEscape(字节serialData,楼盘字节serialResult) { 如果(((SerialProcessValue)serialData == SerialProcessValue.SERIAL_PACKET_START)|| ((SerialProcessValue)serialData == SerialProcessValue.SERIAL_PACKET_END)){ serialEscaped = FALSE; serialResult = 0; 回报(真); } }否则,如果(serialEscaped){ 如果((SerialProcessValue)serialData == SerialProcessValue.SERIAL_PACKET_ESCAPE_START){ serialResult =(INT) SerialProcessValue.SERIAL_PACKET_START; serialEscaped = FALSE; 回报(真); } }其他{ serialResult = serialData; 回报(真); } } 我打电话用的功能引用作为 serialDataLsb &安培; serialDataMsb 。 现在我的困惑左右,这将是 serialDataLsb 或 serialDataMsb ,是否得到的值 serialResult ?? 为(i = 0; I< serialElements;我++){ serialDataLsb = 0; serialDataMsb = 0; ,而(serialBufferWalk< serialIndex){如果(SerialUnEscape(serialBuffer [serialBufferWalk ++],参考serialDataLsb)){中断; } } ,而(serialBufferWalk< serialIndex){如果(SerialUnEscape(serialBuffer [serialBufferWalk ++],参考serialDataMsb)){中断; } } serialElementData [I] =(UINT16)(serialDataLsb +(serialDataMsb<< 8)); } 和我需要移植的代码到Python,1)我怎样才能实现路过的Python参考 我试图用这个 而serialBufferWalk< serialIndex:如果self.SerialUnEscape(serialBuffer [serialBufferWalk + = 1],serialDataLsb):突破,而serialBufferWalk< serialIndex:如果self.SerialUnEscape(serialBuffer [serialBufferWalk + = 1],serialDataLsb):突破 解决方案 如果你真的想模仿传递按引用而不是改变你的代码返回值,你可以用你想在一个对象来改变原始。为简单起见,我用一个列表: 高清change_reference(byteContainer): byteContainer [0] = 42 B = 123 打印(二)#123打印 #中的数据复制到容器列表。 容器= [B] #传递一个指针列表到函数。 change_reference(集装箱)#取值从容器中。 B =容器[0] 打印(二)#打印42 这使你的功能但实际上混淆。你真正应该做的是包括在返回值修改后的字节: 高清test_and_subtract(V):如果v == 1:返回(v - 1,真)返回(v - 2,假) V = 1 V,b = test_and_subtract(五) 打印(五)#0 打印(二)#真 V = 5 V,b = test_and_subtract(v)打印(五)# 3 打印(二)#假 下面收益(v - 1,真)是把两种结果变成一个元组,而 b,v 是从元组删除它们。 I find it really confusing to understand pass by reference in c#. In my code I have function which takes two parameters private bool SerialUnEscape(byte serialData, ref byte serialResult) { if (((SerialProcessValue)serialData == SerialProcessValue.SERIAL_PACKET_START) || ((SerialProcessValue)serialData == SerialProcessValue.SERIAL_PACKET_END)) { serialEscaped = false; serialResult = 0; return (true); } } else if (serialEscaped) { if ((SerialProcessValue)serialData == SerialProcessValue.SERIAL_PACKET_ESCAPE_START) { serialResult = (int)SerialProcessValue.SERIAL_PACKET_START; serialEscaped = false; return (true); } } else { serialResult = serialData; return (true); } }I am calling the function with a reference as serialDataLsb & serialDataMsb.Now my confusion is about, what would be the value of serialDataLsb or serialDataMsb,Does it get the value of serialResult ?? for (i = 0; i < serialElements; i++) { serialDataLsb = 0; serialDataMsb = 0; while (serialBufferWalk < serialIndex) { if (SerialUnEscape(serialBuffer[serialBufferWalk++], ref serialDataLsb)) { break; } } while (serialBufferWalk < serialIndex) { if (SerialUnEscape(serialBuffer[serialBufferWalk++], ref serialDataMsb)) { break; } }serialElementData[i] = (UInt16)(serialDataLsb + (serialDataMsb << 8));}and i need to port this code into python, 1) how can i implement the pass by reference in python I tried using this while serialBufferWalk < serialIndex: if self.SerialUnEscape(serialBuffer[serialBufferWalk += 1],serialDataLsb): breakwhile serialBufferWalk < serialIndex: if self.SerialUnEscape(serialBuffer[serialBufferWalk += 1],serialDataLsb): break 解决方案 If you really want to imitate pass-by-reference instead of changing your code to return the value, you can wrap the primitive you want to change in an object. For simplicity, I used a list:def change_reference(byteContainer): byteContainer[0] = 42b = 123print(b) # Prints 123# Copy the data into the container list.container = [b]# Pass a pointer to that list into the function.change_reference(container)# Take the value out of the container.b = container[0]print(b) # Prints 42This makes your function really confusing though. What you should really do is include the modified byte in the return value:def test_and_subtract(v): if v == 1: return (v - 1, True) return (v - 2, False)v = 1v, b = test_and_subtract(v)print(v) # 0print(b) # Truev = 5v, b = test_and_subtract(v)print(v) # 3print(b) # FalseHere return (v - 1, True) is putting both results into a tuple, and b, v is removing them from that tuple. 这篇关于麻烦参照理解传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-29 12:17