2017-05-09 1 views

답변

4

설명 된 요구 사항은 복사 된 속성에 대해 정의 된 조건부입니다. 이됩니다

class MyClass(object): 
    def __init__(self): 
     super(MyClass, self).__init__() 
     self.foo = 1 
     self.__bar = 2 

def key_predicate(key): 
    return not key.startswith('_') 

obj = MyClass() 
d = {k: v for k, v in obj.__dict__.items() if key_predicate(k)} 

: 이것은 당신이 __* 속성을 생략 할 가정, 키를 통해 필터링 할 수 있습니다

{'foo': 1} 

이것은 새로운 인스턴스에 적용 할 수 있습니다

class MyOtherClass(object): 
    pass 

other_obj = MyOtherClass() 
other_obj.__dict__.update(d) 
assert other_obj.foo == 1 
+0

그것은 작동합니다. 고마워요! – natsuapo

관련 문제