Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

676
Views
Updating the values of variables inside a namedtuple() structure

I want to create a structure, as we do in C, in python. I have tried to use namedtuple() for this. However, I can not update the values of variables I have described inside the custom structure. Here is how i declared it:

from collections import namedtuple as nt
Struct = nt('Struct','all left right bottom top near far')

And this is what i am trying to do in a method :

class codeClip:
    def compOutCode(x,y,z,xmin,xmax,ymin,ymax,zmin,zmax):
        code = Struct(0,0,0,0,0,0,0)  
        if(y > ymax):
            code.top = 1
            code.all += code.top
        elif(y < ymin):
            code.bottom = 1            
        return code

However it is giving this error:

code.top = 1 AttributeError: can't set attribute

What should I do? Pardon me, I am fairly new in python, so still getting used to all of these.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You may use the _replace() method.

Instead of code.top = 1, you can update values as follows.

code = code._replace(top = 1)

Please note, named tuples are immutable, so you cannot manipulate them. If you want something mutable, you can use recordtype.

Reference: https://stackoverflow.com/a/31253184/5352399

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!