1

If I have a script like:

class ClassA(object):
    ...
    def methodA(...):
        varA=...

        def funcA(...):
            ...

        varA=funA(...)

I mean I intend to write a small function inside a class method, which is only used inside this class method. Is this code style OK? I think it's a little ugly. Is there alternate?

11
  • 1
    One potential problem is that you'll be re-defining funcA every time methodA is called. Commented Dec 21, 2012 at 6:55
  • Have you considered using a lambda function instead? Commented Dec 21, 2012 at 6:56
  • @Blender then how can I avoid re-defining funcA? Commented Dec 21, 2012 at 6:59
  • @ThunderEX: By defining it outside of methodA. Commented Dec 21, 2012 at 7:00
  • 1
    See this question from just an hour ago for some discussion. Commented Dec 21, 2012 at 7:19

1 Answer 1

1

Beauty is in the eye of the beholder.

It is totally fine having a very "local" function nested inside another function - especially from the point of readability of code.

Others will argue with coding style and best practice.

It is your code and you must feel fine with your code in order to understand and read it later.

So if it is fine for you and your understanding of "nice" code, go ahead.

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

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.