turbonate turbonate - 9 months ago 34
Python Question

Python function call within a property of a class

I am trying to use the

"setx"
function of a
Property
in a
Class
to do some processing of date information that I get from excel. I have a few of my own functions that do the data processing which I tested outside the class, and they worked just fine. But when I move them into the class they suddenly become invisible unless I use the
self.
instance first. When I use the
self.My_xldate_as_tuple()
method I get an error:

My_xldate_as_tuple() takes 1 positional argument but 2 were given


Even though the code is EXACTLY what i used outside the class before and it worked.

Before moving into the
Property Set
block, I was doing the processing of date data outside of the class and setting the variables from outside of the class. That gets clunky when I have about 15 different operations that are all based on when the
NumDates Property
change. I'm showing shortened versions of both the working set of code and the non-working set of code. What is going on with the
self.
call that changes how the function takes inputs?

Broken Code:

class XLDataClass(object):

_NumDates = []
TupDates = []

def getNumDates(self): return self._NumDates
def setNumDates(self, value):
self._NumDates = value
self.TupDates = list(map(self.My_xldate_as_tuple,value)) #Error here

#This version doesn't work either, since it can't find My_xldate_as_tuple anymore
self.TupDates = list(map(My_xldate_as_tuple,value))

def delNumDates(self):del self._NumDates
NumDates = property(getNumDates,setNumDates,delNumDates,"Ordinal Dates")

#exact copy of the My_xldate_as_tuple function that works outside the class
def My_xldate_as_tuple(Date):
return xlrd.xldate_as_tuple(Date,1)

#Other code and functions here
#end XlDataClass

def GetExcelData(filename,rowNum,titleCol):
csv = np.genfromtxt(filename, delimiter= ",")
NumDates = deque(csv[rowNum,:])
if titleCol == True:
NumDates.popleft()
return NumDates

#Setup
filedir = "C:/Users/blahblahblah"
filename = filedir + "/SamplePandL.csv"
xlData = XLDataClass()

#Put csv data into xlData object
xlData.NumDates= GetExcelData(filename,0,1)


Working Code:

class XLDataClass(object):

NumDates = []
TupDates = []

#Other code and functions here
#end XlDataClass

#exact copy of the same function outside of the class, which works here
def My_xldate_as_tuple(Date):
return xlrd.xldate_as_tuple(Date,1)

def GetExcelData(filename,rowNum,titleCol):
csv = np.genfromtxt(filename, delimiter= ",")
NumDates = deque(csv[rowNum,:])
if titleCol == True:
NumDates.popleft()
return NumDates

#Setup
filedir = "C:/Users/blahblahblah"
filename = filedir + "/SamplePandL.csv"
xlData = XLDataClass()

#Put csv data into xlData object
xlData.NumDates = GetExcelData(filename,0,1)
#same call to the function that was inside the Setx Property of the class, but it works here.
xlData.TupDates = list(map(self.My_xldate_as_tuple,value))

Answer Source

Instance methods in Python require an explicit self in the argument list. Inside the class, you need to write your method definition like:

def My_xldate_as_tuple(self, Date):