1

私はこの機能を持っています(説明を含む):

def deep_list(x):
    """fully copies trees of tuples to a tree of lists.
    deep_list( (1,2,(3,4)) ) returns [1,2,[3,4]]"""
    if type(x)!=type( () ):
        return x
    return map(deep_list,x)

その関数を自分で作成した関数クラスに挿入したいのでself、最初に関数の引数に追加する必要があります。

self私の問題はこれです:?の最後にある'map'関数に正しい方法で挿入するにはどうすればよいdeep_listですか?

4

2 に答える 2

4

クラスとの関係によって異なりxます。

1つの方法は、関数を静的メソッドにすることです。これはおそらく最も可能性が低いです

@staticmethod
def deep_list(x):
    """fully copies trees of tuples to a tree of lists.
       deep_list( (1,2,(3,4)) ) returns [1,2,[3,4]]"""
    if type(x)!=type( () ):
        return x
    return map(deep_list,x)

属性を操作する場合は、このようにしてください

def deep_list(self):
    """fully copies trees of tuples to a tree of lists.
       deep_list( (1,2,(3,4)) ) returns [1,2,[3,4]]"""
    if type(self.x)!=type( () ):
        return self.x
    return map(deep_list, self.x)

list最後に、クラスのようなシーケンスをサブクラス化または作成している場合は、self

def deep_list(self):
    """fully copies trees of tuples to a tree of lists.
       deep_list( (1,2,(3,4)) ) returns [1,2,[3,4]]"""
    if type(self)!=type( () ):
        return self
    return map(deep_list, self)
于 2012-11-25T23:58:34.727 に答える
1

あなたが何を求めているのか理解できるかどうかはわかりませんが、バインドされたメソッドをマップすると、自己はすでに含まれています。

>>> class Foo(object):
...     def func(self, x):
...         return x + 2
>>> f = Foo()
>>> map(f.func, [1, 2, 3])
[3, 4, 5]
于 2012-11-25T23:56:20.840 に答える