【发布时间】:2026-02-09 19:20:09
【问题描述】:
Python 有 spwd 模块来与影子密码文件交互。
您必须是 root 才能使用 spwd 模块。
>>> import spwd;
>>> a = spwd.getspnam('bin')
>>> type(a)
<class 'spwd.struct_spwd'>
>>> a.__gt__
<method-wrapper '__gt__' of spwd.struct_spwd object at 0x7fd017641b80>
现在我想为对象“a”(或类“struct_spwd”)添加一个新属性。我该怎么做?
继承不起作用。
import spwd
class Abc(spwd.struct_spwd):
pass
我收到“TypeError: type 'spwd.struct_spwd' is not an accepted base type”。
向对象添加动态属性不起作用。
import spwd
a = spwd.getspnam('bin')
a.new_attribute = lambda: print('hello world')
我收到“AttributeError: 'spwd.struct_spwd' 对象没有属性 'new_attribute'”。
不过,使用委托可能会奏效。但我想保留它原有的属性。
import spwd
class Abc(object):
def __init__(self, struct_spwd):
self.struct_spwd = struct_spwd
def __getattribute__(self, name):
if name=='new_attribute':
print('hello world')
else:
self.struct_spwd.__getattribute__(name)
a = spwd.getspnam('bin')
b = Abc(a)
b.new_attribute
b.__gt__
我得到“RuntimeError:比较中超出了最大递归深度”。
有什么想法吗?还是我应该使用其他方式来实现我的目标?谢谢。
【问题讨论】:
标签: python