python class method invocation missing 1 required positional argument

Question:

I created class method to ease file reading. But the end result gives me the following error message:

TypeError: Read_file.file_reading_through_class() missing 1 required positional argument: 'fileName'

My code is below:

import pandas as pd
import sys,os, time,re
   
class Read_file():
    def file_reading_through_class(self,fileName):
        if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'):
            path_actual = os.getcwd()
            path_main_folder = path_actual[:-4]
            path_result = path_main_folder + fileName
            print('frozen path', os.path.normpath(path_result))
            file_to_read = pd.read_json(path_result)
            return file_to_read
        else:
            file_to_read = pd.read_json(fileName)
            return file_to_read
   
file_read=Read_file.file_reading_through_class('./ConfigurationFile/configFile.csv')
Asked By: xlmaster

||

Answers:

You will need to instantiate the class first.
self refers to the instance of the class.

rf = Read_file()
file_read = rf.file_reading_through_class('./ConfigurationFile/configFile.csv')

Or you can mark your function as a static method, and you can use your function the way you are using it.

@staticmethod
def file_reading_through_class(fileName):
Answered By: krishnasys

You calling the method as if it was a static method.

The following code will work:

import sys, os, time, re    
import pandas as pd



class Read_file():
    def file_reading_through_class(self,fileName):
        if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'):
            path_actual = os.getcwd()
            path_main_folder = path_actual[:-4]
            path_result = path_main_folder + fileName
            print('frozen path', os.path.normpath(path_result))
            file_to_read = pd.read_json(path_result)
            return file_to_read
        else:
            file_to_read = pd.read_json(fileName)
            return file_to_read

reader = Read_file()
file_read = reader.file_reading_through_class('./ConfigurationFile/configFile.csv')

Or if you really want it to be a static method then you can use this code:

import pandas as pd
import sys, os, time, re


class Read_file():
    @staticmethod
    def file_reading_through_class(fileName):
        if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'):
            path_actual = os.getcwd()
            path_main_folder = path_actual[:-4]
            path_result = path_main_folder + fileName
            print('frozen path', os.path.normpath(path_result))
            file_to_read = pd.read_json(path_result)
            return file_to_read
        else:
            file_to_read = pd.read_json(fileName)
            return file_to_read

file_read=Read_file.file_reading_through_class('./ConfigurationFile/configFile.csv')
Answered By: Jens Lordén

Attribute references

In the link proposed by the comment of @chepner it is explained a different way to access to an attribute of a class:

MyClass.i and MyClass.f are valid attribute references, returning an integer and a function object, respectively.

The documentation calls this operation supported by a Class Attribute references while calls the access by an instance Class instantiation.

Adapting the Attribute references to your code we obtain:

file_read=Read_file.file_reading_through_class(Read_file,'./ConfigurationFile/configFile.csv')

Where by Read_file.file_reading_through_class we obtain a reference to the attribute file_reading_through_class that is a function object.
Furthermore the previous instruction executes the function passing to it 2 parameters:

  • Read_file for argument self (this is the the missing argument that you cite in the title of your question)
  • './ConfigurationFile/configFile.csv' for argument fileName.

I can divide the previous instruction in two instructions fully equivalent:

func_reference = Read_file.file_reading_through_class
file_read = func_reference(Read_file, './ConfigurationFile/configFile.csv')

where I have highlighted the reference to the attribute file_reading_through_class.

Answered By: frankfalse
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.