0

I am creating objects that I will eventually graph and using an api for the data. For the object attribute that I intend to graph, I am using a lambda function so that it will return the particular data I need for each new object.

The Lambda function passes two arguments through the api call, the first a, references the objects .name attribute, and works perfectly. When I try to reference the second lambda argument b which references the objects .time attribute, I get an error b is an invalid key argument for __new__()

default = 'USD'
asset1_input = 'ETH'
asset2_input = 'DAI'


class Asset:
    def __init__(self, name, time, allocate, perform):
        self.name = name
        self.time = time
        self.allocate = allocate
        self.perform = perform(name, time)[name][default]

perform = lambda a, b : cryptocompare.get_historical_price(a, 'USD', datetime.today() - timedelta(b=5))


asset1 = Asset(asset1_input, 'days', .2, perform)

print(asset1.perform) 

datetime is expecting days or weeks. If I manually put one of these in then it works fine. Part of the problem may be that datetime isn't expecting a string days, its just expecting days, however when I put days in as the value for time, I get days undefined. Was considering an if statement and a for loop in the lambda function (if some input == 'days', and a for loop to iterate the days value) however other stack posts mentioned that you cant iterate with a for loop in lambda function.

Rolv Apneseth
  • 2,078
  • 2
  • 7
  • 19
hughm-01
  • 35
  • 5
  • 2
    You may as well make `perform` a normal boring old function because a) then you can debug it, and b) you can add if/for/whatever control flow you need. But otherwise show the full error message including stack trace and the source code covering that if not already in your question. – DisappointedByUnaccountableMod Feb 10 '21 at 14:11
  • 1
    Your question is unclear - you should really consider creating a [mre]. But maybe what you mean is what is suggested here: https://stackoverflow.com/a/11441779/550094 (I'm sure there are better duplicates somewhere, but I fail to find any right now...) – Thierry Lathuille Feb 10 '21 at 14:12
  • @barny `perform` *is* a normal boring old function. It's just defined (inadvisedly) using a lambda expression instead of a `def` statement. – chepner Feb 10 '21 at 15:56

0 Answers0