1

I have a function that takes *arg and an additional positional argument. but when calling this function I am getting below error. Any help how to pass this additional argument

**error**:
export_bundle() missing 1 required keyword-only argument: 'dict'
from stix2 import MemoryStore, Identity
import datetime

timestamp = datetime.datetime.today().strftime('%Y-%m-%d-%H:%M:%S')

id1 = Identity(
    name="John Smith",
    identity_class="individual",
    description="Just some guy",
)
id2 = Identity(
    name="John Smith",
    identity_class="individual",
    description="A person",
)

dict= {"id":1234, "name":"abd"}

def export_bundle(self, *args, dict):
   mem = MemoryStore()
   for sdo in args:
      mem.add([sdo])
   mem.save_to_file(self.output_location + str(dict['id']) + timestamp+ '.json')
    del mem

export_bundle(id1, id2, dict)
2
  • 3
    Read the error message carefully: missing a keyword-only argument. Commented Sep 28, 2020 at 4:00
  • You can't define a function that takes positional arguments after *args. Commented Sep 28, 2020 at 4:11

2 Answers 2

3

You declared the function export_bundle with variable number of arguments (*args), so if you want to define the dict argument at the end it needs to be keyword-only parameter (dict). You can call it as export_bundle(id1, id2, dict=dict) if you want to pass a value for dict.

def export_bundle(self, *args, dict):
   mem = MemoryStore()
   for sdo in args:
      mem.add([sdo])
   mem.save_to_file(self.output_location + str(dict['id']) + timestamp+ '.json')
   del mem

export_bundle(id1, id2, dict=dict)
Sign up to request clarification or add additional context in comments.

5 Comments

Adding =None in the function definition does not make the argument keyword-only. Putting it after a * argument is what makes it keyword-only. =None in the definition gives the argument a default value, which is a completely unrelated thing.
thing=whatever in a function call passes an argument by keyword. thing=whatever in a function definition gives the argument a default value. These two things are 100% unrelated, but happen to use confusingly similar syntax.
@user2357112supportsMonica You're right. I just tested it and it seems to be the case. I've removed the =None for minimizing changes from OP's code
I am getting below error as well when I called the function as you have suggested: 'dict' object has no attribute 'output_location' . There must be some way to pass self argument
@st_bones The error you're getting doesn't seem to be related to your previous error. It's because you're accessing the property output_location of self parameter, in this case id1, which doesn't exist in Identity object. docs.oasis-open.org/cti/stix/v2.1/cs01/…
0

Always function parameters first & then *args, **kwargs

def export_bundle(self, dictm,*args):
   mem = MemoryStore()
   for sdo in args:
      mem.add([sdo])
   mem.save_to_file(self.output_location + str(dict['id']) + timestamp+ '.json')
    del mem```

1 Comment

I am getting below error when I called the function as you have suggested: 'dict' object has no attribute 'output_location' . There must be some way to pass self argument?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.