1

I have a method get_annotated_pkt_msg() which takes a boolean parameter show_timestamp. I want this to be an optional parameter, and so that if the caller doesn't specify an argument it will default to what is set in the user-defined config. This config is stored in container self.config which has been passed-in by constructor dependency injection:

class XcpDaqFrame(XcpFrame):

    def __init__(self, *args, **kwargs):
        # Pass init arguments to super-class.
        super(XcpDaqFrame, self).__init__(*args, **kwargs)
        # Passed by dependency injection to this constructor.
        self.config = config

    def get_annotated_pkt_msg(
            self,
            show_timestamp=self.config.getConfigItem('display.packet_time')):
        ##### SyntaxError here ^ (on the dot after 'self') ########
        """Return the annotated packet message

        :param bool show_timestamp:
        :rtype: str
        """
        # Optionally, show the timestamp of the packet.
        if show_timestamp:
            timestamp = get_timestamp()
            timestamp_msg = u", Timestamp: {} μs".format(timestamp)
        else:
            timestamp_msg = ""
        return timestamp_msg

frame = XcpDaqFrame(my_config)
frame.get_annotated_pkt_msg()

If I try the above it tells me, on the line marked above:

SyntaxError: invalid syntax

Why is it that I can pass self to methods, but can't pass them self.config?

DBedrenko
  • 4,871
  • 4
  • 38
  • 73
  • 1
    `self` is not defined. You should use `None` as the default then set it to what you want if it really is `None`. – Huazuo Gao Sep 25 '15 at 09:19
  • @HuazuoGao It's weird that self *IS* defined in the first parameter, though. Thanks for the workaround, I just thought it would be more explicit to say specifically where the value is coming from in the method's header. – DBedrenko Sep 25 '15 at 09:22
  • 2
    Default arguments is not evaluated at call, but at **definition**. `self` has no value at definition time, so it won't work that way. – Huazuo Gao Sep 25 '15 at 09:24

1 Answers1

3

The default parameters for a function are evaluated when the function is defined, not when the function gets called, so at the time of definition of the function (self has no value) , and you cannot use other parameters to the same function in default parameters. This also leads to other gotchas like mutable default argument (Read more about that here) .

Instead of that, you can try using some other default value (like None ) or so, and then default it to self.config.getConfigItem('display.packet_time') , if its None . Example -

def get_annotated_pkt_msg(
        self,
        show_timestamp=None):
    ##### SyntaxError here ^ (on the dot after 'self') ########
    """Return the annotated packet message

    :param bool show_timestamp:
    :rtype: str
    """
    if show_timestamp is None:
        show_timestamp = self.config.getConfigItem('display.packet_time')
    # Optionally, show the timestamp of the packet.
    ... #Rest of the logic
Community
  • 1
  • 1
Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176