简体   繁体   English

2个功能输入?

[英]2 inputs to a function?

So Ive been giving the following code in a kind of sort of python class. 所以我一直在一种python类中提供以下代码。 Its really a discrete math class but he uses python to demonstrate everything. 它真的是一个离散的数学课,但他使用python来演示一切。 This code is supposed to demonstate a multiplexer and building a xor gate with it. 该代码应该用于演示多路复用器并构建xor门。

def mux41(i0,i1,i2,i3):
    return lambda s1,s0:{(0,0):i0,(0,1):i1,(1,0):i2,(1,1):i3}[(s1,s0)]

def xor2(a,b):
    return mux41(0,1,1,0)(a,b)

In the xor2 function I dont understand the syntax behind return mux41(0,1,1,0)(a,b) the 1's and 0's are the input to the mux function, but what is the (a,b) doing? xor2函数中,我不理解return mux41(0,1,1,0)(a,b)背后的语法return mux41(0,1,1,0)(a,b) 1和0是多路复用函数的输入,但是(a,b)是做什么的?

The (a, b) is actually the input to the lambda function that you return in the mux41 function. (a, b)实际上是你在mux41函数中返回的lambda函数的输入。

Your mux41 function returns a lambda function which looks like it returns a value in a dictionary based on the input to the mux41 function. 你的mux41函数返回一个lambda函数,它看起来像是根据mux41函数的输入在字典中返回一个值。 You need the second input to say which value you want to return. 您需要第二个输入来说明要返回的值。

It is directly equivalent to: 它直接相当于:

def xor2(a,b):
    f = mux41(0,1,1,0)
    return f(a,b)

That is fairly advanced code to throw at Python beginners, so don't feel bad it wasn't obvious to you. 这是相当高级的代码,以抛出Python初学者,所以不要感到不好,这对你来说并不明显。 I also think it is rather trickier than it needs to be. 我也认为它比它需要的更棘手。

def mux41(i0,i1,i2,i3):
    return lambda s1,s0:{(0,0):i0,(0,1):i1,(1,0):i2,(1,1):i3}[(s1,s0)]

This defines a function object that returns a value based on two inputs. 这定义了一个函数对象,它根据两个输入返回一个值。 The two inputs are s1 and s0 . 两个输入是s1s0 The function object builds a dictionary that is pre-populated with the four values passed int to mux41() , and it uses s0 and s1 to select one of those four values. 函数对象构建一个字典,该字典预先填充了传递给mux41()的四个值,并使用s0s1来选择这四个值中的一个。

Dictionaries use keys to look up values. 字典使用键来查找值。 In this case, the keys are Python tuples: (0, 0) , (0, 1) , (1, 0) , and (1,1) . 在这种情况下,键是Python元组: (0, 0)(0, 1)(1, 0)(1,1) The expression (s1,s0) is building a tuple from the arguments s0 and s1 . 表达式(s1,s0)正在从参数s0s1构建一个元组。 This tuple is used as the key to lookup a value from the dictionary. 此元组用作从字典中查找值的键。

def xor2(a,b):
    return mux41(0,1,1,0)(a,b)

So, mux41() returns a function object that does the stuff I just discussed. 因此, mux41()返回一个函数对象,它执行我刚刚讨论的内容。 xor2() calls mux41() and gets a function object; xor2()调用mux41()并获取一个函数对象; then it immediately calls that returned function object, passing in a and b as arguments. 然后它立即调用返回的函数对象,传入ab作为参数。 Finally it returns the answer. 最后它返回答案。

The function object created by mux41() is not saved anywhere. mux41()创建的函数对象不会保存在任何位置。 So, every single time you call xor2() , you are creating a function object, which is then garbage collected. 因此,每次调用xor2() ,都会创建一个函数对象,然后进行垃圾回收。 When the function object runs, it builds a dictionary object, and this too is garbage collected after each single use. 当函数对象运行时,它会构建一个字典对象,这也是每次使用后的垃圾收集。 This is possibly the most complicated XOR function I have ever seen. 这可能是我见过的最复杂的XOR功能。

Here is a rewrite that might make this a bit clearer. 这是一个重写,可能会使这一点更清晰。 Instead of using lambda to create an un-named function object, I'll just use def to create a named function. 我将使用def来创建一个命名函数,而不是使用lambda来创建一个未命名的函数对象。

def mux41(i0,i1,i2,i3):
    def mux_fn(s1, s0):
        d = {
            (0,0):i0,
            (0,1):i1,
            (1,0):i2,
            (1,1):i3
        }
        tup = (s1, s0)
        return d[tup]
    return mux_fn

def xor2(a,b):
    mux_fn = mux41(0,1,1,0)
    return mux_fn(a,b)

EDIT: Here is what I would have written if I wanted to make a table-lookup XOR in Python. 编辑:如果我想在Python中进行表查找XOR,这就是我想写的。

_d_xor2 = {
    (0,0) : 0,
    (0,1) : 1,
    (1,0) : 1,
    (1,1) : 0
}

def xor2(a,b):
    tup = (a, b)
    return _d_xor2[tup]

We build the lookup dictionary once, then use it directly from xor2() . 我们构建一次查找字典,然后直接从xor2()使用它。 It's not really necessary to make an explicit temp variable in xor2() but it might be a bit clearer. xor2()创建一个显式的临时变量并不是必需的,但它可能会更清晰一点。 You could just do this: 你可以这样做:

def xor2(a,b):
    return _d_xor2[(a, b)]

Which do you prefer? 你喜欢哪个?

And of course, since Python has an XOR operator built-in, you could write it like this: 当然,由于Python内置了一个XOR运算符,你可以像这样写:

def xor2(a,b):
    return a ^ b

If I were writing this for real I would probably add error handling and/or make it operate on bool values. 如果我真的写这个,我可能会添加错误处理和/或使其对bool值进行操作。

def xor2(a,b):
    return bool(a) ^ bool(b)

EDIT: One more thing just occurred to me. 编辑:还有一件事发生在我身上。 In Python, the rule is "the comma makes the tuple". 在Python中,规则是“逗号制作元组”。 The parentheses around a tuple are sometimes optional. 元组周围的括号有时是可选的。 I just checked, and it works just fine to leave off the parentheses in a dictionary lookup. 我刚检查过,它可以正常工作,在字典查找中省略括号。 So you can do this: 所以你可以这样做:

def xor2(a,b):
    return _d_xor2[a, b]

And it works fine. 它工作正常。 This is perhaps a bit too tricky? 这可能有点太棘手了? If I saw this in someone else's code, it would surprise me. 如果我在别人的代码中看到这个,那会让我感到惊讶。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM