1

I have the following function, I want to concat the 2 strings, What wrong am I doing here?

commands = ["abcd","123"]

def configure_dev(self, steps):
    func_name = self.id + ':configure dev'

    global conf_cmd
    for key in commands:
        conf_cmd += key + '\n'
    print(conf_cmd)

Getting the following error:

conf_cmd += key + '\n'

After running it, I get this error: NameError: name 'conf_cmd' is not defined

3
  • 3
    Where have you defined conf_cmd? global does not create a new variable. Commented Jul 15, 2016 at 18:44
  • 1
    you set it as global but it is undefined. Commented Jul 15, 2016 at 18:45
  • Thanks that was silly of me. Commented Jul 15, 2016 at 18:48

2 Answers 2

1

I added your code with your critical issue resolved.

commands = ["abcd","123"]
def configure_dev(self, steps):
  func_name = self.id + ':configure dev'
  global conf_cmd = ''  //  <-- ''
  for key in commands:
    conf_cmd+=key+'\n'
  print(conf_cmd)
Sign up to request clarification or add additional context in comments.

2 Comments

Keep in mind that every call to configure_dev will empty the string.
true. Im not sure what his expectation was, but under the expectation of his coding, each time that function was called, he was planning to reinitialize it. Or at least that is what i picked up from their code.
1

All you need to do is to add: conf_cmd = ''

just after commands = ["abcd","123"]

Why? global conf_cmd Does not create new string, it just means you can access the global variable.

Comments

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.