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')
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):
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')
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
.
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')
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):
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')
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 argumentself
(this is the the missing argument that you cite in the title of your question)'./ConfigurationFile/configFile.csv'
for argumentfileName
.
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
.