1

I want to change a Python 2 code to Python 3. In particular, I would like to change a.itervalues to a.values.

I would to know if it is possible to define a new function so that when the program comes across .itervalues, it will run my new function instead of its original one?

Thank you very much.

2
  • 3
    have you tried this first, docs.python.org/3/library/2to3.html Commented Jul 26, 2018 at 10:39
  • You want a 3rd function, that will call itervalues or values depending on your python version? Commented Jul 26, 2018 at 10:44

1 Answer 1

1

as @aws_apprentice said : go to https://docs.python.org/3/library/2to3.html

specifically read this section :

dict Fixes dictionary iteration methods. dict.iteritems() is converted to dict.items(), dict.iterkeys() to dict.keys(), and dict.itervalues() to dict.values(). Similarly, dict.viewitems(), dict.viewkeys() and dict.viewvalues() are converted respectively to dict.items(), dict.keys() and dict.values(). It also wraps existing usages of dict.items(), dict.keys(), and dict.values() in a call to list.

so all you need to do is run 2to3 script on your files and you customize your code to suit Python 3 convention.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, ddor254. The problem can be solved by 2to3. However, I am interested to know how to replace a function by another. Can we use the idea of decorator?

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.