0

I have this type of function written:

def sendOrder(self,symbol, orderType, volume, openPrice, slippage = 0, stopLoss = 0, takeProfit = 0, comment = None, magic = 0, expiration = 0) :

        sendOrder = {"messageType": "sendOrder", **locals()}
        print(sendOrder)

It prints this:

{'messageType': 'sendOrder', 'self': <__main__.forexPlatformBridge object at 0x03077100>, 'symbol': 'EURUSD', 'orderType': 'buy', 'volume': 0.5, 'openPrice': 1.115, 'slippage': 0, 'stopLoss': 0, 'takeProfit': 0, 'comment': None, 'magic': 0, 'expiration': 0}

How do I remove the 'self' key? Is there a way to make a python dict out of parameters without including 'self' and adding 'messageType'

The desired output would be:

{'messageType': 'sendOrder', 'symbol': 'EURUSD', 'orderType': 'buy', 'volume': 0.5, 'openPrice': 1.115, 'slippage': 0, 'stopLoss': 0, 'takeProfit': 0, 'comment': None, 'magic': 0, 'expiration': 0}
blennd
  • 71
  • 6
  • 3
    this is a question of how to remove an key/value pair from a dictionary -- there are lots of answers to that online. try `del sendOrder["self"]` before your print function – jkr May 10 '20 at 21:10
  • Can you mention the desired output? – techytushar May 10 '20 at 21:10
  • `sendOrder.pop('self')` ? – trsvchn May 10 '20 at 21:13
  • The dictionary is invalid. I think the value of `key - self` needs to be in a quote? – coldy May 10 '20 at 21:15
  • Be explicit, and list the keys in the dict. The caller will likely do something like `self.sendOrder(**somePrepackedDict)` anyway, so you might want to change the signature to take a single argument, typehinted with a subclass of `typing.TypeHint`. – chepner May 10 '20 at 21:31
  • You only have to write the definition once: err on the side of readability. – chepner May 10 '20 at 21:32

0 Answers0