Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

682
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda