close

[Solved] Attribute: can’t set attribute in python

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Attribute: can’t set attribute in python in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Attribute: can’t set attribute in python Error Occurs?

Today I get the following error Attribute: can’t set attribute in python in python.

How To Solve Attribute: can’t set attribute in python Error ?

  1. How To Solve Attribute: can't set attribute in python Error ?

    To Solve Attribute: can't set attribute in python Error Edit: if you want the latter, Ignacio's answer does the same thing more neatly using baked-in functionality.

  2. Attribute: can't set attribute in python

    To Solve Attribute: can't set attribute in python Error Edit: if you want the latter, Ignacio's answer does the same thing more neatly using baked-in functionality.

Solution 1

items[node.ind] = items[node.ind]._replace(v=node.v)

(Note: Don’t be discouraged to use this solution because of the leading underscore in the function _replace. Specifically for namedtuple some functions have leading underscore which is not for indicating they are meant to be “private”)

Solution 2

namedtuples are immutable, just like standard tuples. You have two choices:

  1. Use a different data structure, e.g. a class (or just a dictionary); or
  2. Instead of updating the structure, replace it.

The former would look like:

class N(object):

    def __init__(self, ind, set, v):
        self.ind = ind
        self.set = set
        self.v = v

And the latter:

item = items[node.ind]
items[node.ind] = N(item.ind, item.set, node.v)

Edit: if you want the latter, Ignacio’s answer does the same thing more neatly using baked-in functionality.

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read