2

File structure:

def mainStuff():
  for a in aa:
    aName = aa[a].split('')[-1].split('-')[5]

   # Do other important stuff so I can't return early

def replaceOne():
  if aName.split('_')[1] == "android":
    aName = aName.replace('_android','Android')

  return aName

def replaceTwo():
  if aName.split('_')[4:7] == "abc":
    aName = aName.replace('abc','Tom')

  return aName

I want the two if statement blocks to run consecutively, so I have decided to put them in separate functions. However, as aName is generated when the loop runs, my functions are unable to get the variable. I cannot use return as the function has to run to its completion for my output to work.

I have tried using yield but the function just ends prematurely.

How do I get aName and pass it through my other functions?

5
  • @deceze I have added the file structure, how does it look now? Commented Jul 8, 2019 at 8:52
  • 1
    Revise if aName.split('_')[4:7] == "abc": , it seems dont do what you want to do Commented Jul 8, 2019 at 8:59
  • Oh I just added random symbols in there, please don't mind. I'm using other symbols that are not - or _ @Wonka Commented Jul 8, 2019 at 9:00
  • @Yuu your question is about fundamental python syntax, i.e. how to pass parameters to functions, and you should focus on understanding that properly. But once you have this working, you might want to looking into using regex substitution to solve this problem. Commented Jul 8, 2019 at 9:02
  • @Dan Thanks! I will work on my fundamentals. Commented Jul 8, 2019 at 9:06

1 Answer 1

1

You need to pass aName to your functions a parameter. See the parameters section in this tutorial. In your case, try adjusting your code as follows:

def mainStuff():
    for a in aa:
        aName = aa[a].split('')[-1].split('-')[5]
        aName = replaceOne(aName)
        aName = replaceTwo(aName)

def replaceOne(aName: str) -> str:
    if aName.split('_')[1] == "android":
        aName = aName.replace('_android','Android')
    return aName

def replaceTwo(aName: str) -> str:
    if aName.split('_')[4:7] == "abc":
        aName = aName.replace('abc','Tom')
    return aName
Sign up to request clarification or add additional context in comments.

3 Comments

As this is the first time I see this syntax, could you explain what it does? def foo(arg: type) -> type? Is this even valid python?
Wow. That's awesome for making cythoning easier. Thank you!

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.