This function makes a range of dates (this function is related to a class):
def _makeRangeDates(self, desde, ultima_carga):
dates = []
for fechaRequest in rrule(DAILY, dtstart=desde, until=ultima_carga):
dates.append(fechaRequest)
return self.dates
What is the best way to return the list of dates inside of this function?
is mandatory to return self.dates ?
Regards. Nelson.
You can't return self.dates
here. You are appending your dates in dates
variable and that should be returned inside the method. In your case, self.dates
might not even exist.
Edit: I think this is what you are trying to implement
class abc():
def __init__(self):
self.dates = []
def _makeRangeDates(self,desde,ultima_carga):
for fechaRequest in rrule(DAILY, dtstart=desde, until=ultima_carga):
self.dates.append(fechaRequest)
return self.dates
I'm assuming that you've got this function in some sort of class foo
like so:
class foo(object):
def __init__(self):
self.dates = []
def _makeRangeDates(self,desde,ultima_carga):
dates = []
for fechaRequest in rrule(DAILY, dtstart=desde, until=ultima_carga):
dates.append(fechaRequest)
return self.dates
In this case returning dates
and self.dates
are two totally different things. Since dates
is created within the _makeRangeDates
function, it will be garbage collected soon after the function is called. If you're looking to just return dates
and not save it in some state then that's perfect fine.
If you're hoping to save the state of dates
then do the following:
class foo(object):
def __init__(self):
self.dates = []
def _makeRangeDates(self,desde,ultima_carga):
for fechaRequest in rrule(DAILY, dtstart=desde, until=ultima_carga):
self.dates.append(fechaRequest)
return self.dates
You're confusing local variables and data members. And _makeRangeDates
isn't even a method, just a function , since it doesn't read or write the object (you never use the self
parameter).
dates
is just some local variable you created inside your function with:
dates = []
for fechaRequest in rrule(DAILY, dtstart=desde, until=ultima_carga):
dates.append(fechaRequest)
dates
is not a data member, ie it's not self.dates
.
If you just want a function which returns that result, then return dates
. That function can stand outside the class hierarchy.
If you want to put a helper function like this inside a class, but which doesn't actually access the object at all, that sort of function is called a staticmethod / classmethod (suggest you read up about the difference between the two).
The dates
variable and self.dates
variable are two different variables (unless you do some assignment such as self.date = date
, after this assignment they both are referring to the same object (but are still different names/variables pointing to same reference) .
And yes you can return any of them python should not complain (unless you are trying to return a variable that has not been defined/initialized , like you are doing in your example)
In your example you are creating dates
variable and appending values to it, but at the end you are returning self.dates
(which may cause issues since self.dates
may not have been defined when the function is called).
I believe you want to return dates
from your function , since that is the variable which you define in the function and to which you append the dates .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.