Map not calling the function being passed.
class a:
def getDateTimeStat(self,datetimeObj):
print("Hello")
if __name__ == "__main__":
obj = a()
print("prog started")
data = [1,2,3,4,5]
b = list(map(obj.getDateTimeStat,data))
Expected op on a new line: Hello Hello Hello Hello Hello
Any help will be appreciated....
In React, the map() function is most commonly used for rendering a list of data to the DOM. To use the map() function, attach it to an array you want to iterate over. The map() function expects a callback as the argument and executes it once for each element in the array.
The call to map() applies square() to all of the values in numbers and returns an iterator that yields square values. Then you call list() on map() to create a list object containing the square values.
Map in Python is a function that works as an iterator to return a result after applying a function to every item of an iterable (tuple, lists, etc.). It is used when you want to apply a single transformation function to all the iterable elements. The iterable and function are passed as arguments to the map in Python.
map() works way faster than for loop. Considering the same code above when run in this ide. Using map():
In Python 3, map values are evaluated lazily. That is, each value is only computed when it's needed. You'll find that regardless of what function you use, it won't get called until you ask for the value of that item in the mapped result, whether by using next()
or some other way.
To get what you want, you can do this:
>>> b = map(obj.getDateTimeStat,data)
>>> next(b)
Hello
>>> next(b)
Hello
>>> next(b)
Hello
>>> next(b)
Hello
>>> next(b)
Hello
>>> next(b)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
Or this:
>>> b = list(map(obj.getDateTimeStat,data))
Hello
Hello
Hello
Hello
Hello
Or a variety of other things.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With