Python List对象属性'追加'是只读的

时间:2015-10-26 21:09:50

标签: python

正如标题所说,在python中,我试图在有人输入选项(在本例中为Choice13)时这样做,然后从列表密码中删除旧密码并添加新密码。

passwords = ['mrjoebblock' , 'mrjoefblock' , 'mrjoegblock', 'mrmjoeadmin' ]
if choice == '3':
    password = raw_input('Welcome admin! I\'m going to need your password ')
        if password == 'mrjoeadmin':
            print('Welcome Mr. Joe!')
            Choice11 = raw_input('What would you like to do? Press 1 for changing your admin password, 2 for viewing a class\'s comments, or 3 for changing a class\'s password')
            if Choice11 == '1':
                print('You have chosen to change your password! ')
                Choice12 = raw_input('You will need to put in your current password to access this feature ')
                if Choice12 == 'mrmajoeadmin':
                    Choice13 = raw_input('What would you like to change your password to? ')
                    passwords.remove('mrjoeadmin')
                    passwords.append = Choice13

2 个答案:

答案 0 :(得分:16)

要将某些内容附加到列表中,您需要调用 ssl.SSLContext._windows_cert_stores = ("ROOT",) # patch windows_cert_stores default to only include "ROOT" as "CA" is broken for you. #ssl.SSLContext.load_default_certs = lambda s,x:None # alternative, fully NOP load_default_certs to do nothing instead. ctx = ssl.create_default_context() # create new sslcontext, not veryfing any certificates, hostnames. ctx.check_hostname = False ctx.verify_mode = ssl.CERT_NONE hdr = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11'} req = Request('https://' + url, headers=hdr) x = urlopen( req , context=ctx).read() ssl.SSLContext._windows_cert_stores = ("ROOT","CA") # UNDO PATCH 方法:

append

正如您所见,分配给append方法会导致异常,因为您不应该替换内置对象上的方法 - (如果要修改内置类型,则支持这样做的方法是通过子类化。)

答案 1 :(得分:0)

或者您可以通过执行以下操作来修改相同的列表槽:

passwords[passwords.index('mrjoeadmin')] = Choice13