Attributeerror: str Object has no attribute 'num_points'

Asked

Viewed 364 times

1

I am trying to create a data collector of an experiment I am doing. So far I have the following code:

in an archive experiments.py has the class:

import csv

class Experimento:
        
       def __init__(self, material: str, num_points: int, time: float=20.0, expType: str='varredura'):
            
            if expType not in ('varredura','ponto fixo'):
                raise ValueError('Tipo de experimento inválido. Selecione entre Varredura ou Ponto Fixo. \n NOTA: não utilize letras maiúsculas!')
            
            self.num_points = num_points
            self.time = time
            self.material = material
            self.expType = expType

And the subclass:

class Ponto_Fixo(Experimento):
    
    def __init__(self, material: str, num_points: int, x: int, z: int, time: float=20.0):
        Experimento.__init__(material, num_points, time, 'ponto fixo')
        self.x = x
        self.z = z
        self.expID = material + ' x' + str(self.x) + ' z' + str(self.z) + ' pt=' + str(num_points)
        
        header = ['data','material','ponto','x','z','contagem','tempo(s)']
        
        csv_title = '\%s.csv' %(self.expID)
        with open('resultados'+ csv_title,'w') as file:
            writer = csv.writer(file)
            writer.writerow(header)

In the main.py, I run this code like this:

import experiments

if __name__ == "__main__":
    material = 'agua+CaCO3'
    expType = 'ponto fixo'
    tempo = 20.0
    num_points = 6
    x = 2
    z = 2

def new_experiment(material, num_points, tempo, x, z, expType):
        
    if expType == 'ponto fixo':
        
        experiments.Ponto_Fixo(material, num_points, x, z, tempo)
        
novo_exp = new_experiment(material, num_points, tempo, x, z, expType)

However, by doing this execution I get the title error

  File "C:\Adam\Doutorado\Laboratorio\TARG\programs\data collection\main.py", line 46, in <module>
    novo_exp = new_experiment(material, num_points, tempo, x, z, expType)

  File "C:\Adam\Doutorado\Laboratorio\TARG\programs\data collection\main.py", line 37, in new_experiment
    experiments.Ponto_Fixo(material, num_points, x, z, tempo)

  File "C:\Adam\Doutorado\Laboratorio\TARG\programs\data collection\experiments.py", line 113, in __init__
    Experimento.__init__(material, num_points, time, 'ponto fixo')

  File "C:\Adam\Doutorado\Laboratorio\TARG\programs\data collection\experiments.py", line 37, in __init__
    self.num_points = num_points

AttributeError: 'str' object has no attribute 'num_points'

The line that the program accuses the error is this (which is within the Experiment class):

self.num_points = num_points

Also, the line that changes the type of the variable num_point of int for str is after this line of error. Therefore, I am not able to see where and why is taking place the change of type of int for str in the num_point.

Can anyone shed a light? Thank you

1 answer

0


I believe the goal is to access the "father" class, so change:

Experimento.__init__(material, num_points, time, 'ponto fixo')

For super(), thus:

super().__init__(material, num_points, time, 'ponto fixo')

If it’s Python 2 you’ll have to use it like this:

super(Ponto_Fixo, self).__init__(material, num_points, time, 'ponto fixo')

Official documentation: https://docs.python.org/3/library/functions.html#super, that’s where it’s ideal to start learning, syntax, language features and native Apis.

  • while doing what you suggested, at first gave the following error ValueError: __init__() requires a code object with 0 free vars, not 1 However, insisting a little on the execution of the code, the error stopped happening and everything went well. I still don’t really understand why of this, but your help solved the problem. Thank you very much.

  • @Adambasil here his code executed perfectly: https://ideone.com/Vg3MCh -- I only removed the part of CSV pq that cannot read or write files in this online test.

  • @Adambasil must be because you are using jupyter, or anything with autoreloader, sometimes occurs.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.