Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

687
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda