A
A
Artyom Innokentiev2015-10-23 20:40:58
Django
Artyom Innokentiev, 2015-10-23 20:40:58

How should variables and functions be named in Python?

1) Is it worth using variables with such common names: result, data, final?

def foo(questions):
     result = []
     for question in questions:
           data = question
           result.append(data)
     return result

2) Should I use the plural for the variable name: tickets, questions?
for ticket in tickets:
    ....
for question in questions:
   .....

When using a lot of plural variables, it's very difficult to recognize the variables you need - you always have to look at the end of the variable name. As a result, the code becomes not very readable.
3) Is it worth using the exact names of variables, functions: first_question_from_ticket, get_question_from_string?
first_question_from_ticket = data[0]
def get_question_from_string():
     .....

I think, in general, this increases the understanding of what the function does or what this variable is for, but there is a lot of writing - again, as a result, very long and unreadable code.
Please give advice, experienced developers!
PS I will be glad to links to resources with Best Practices on this topic)

Answer the question

In order to leave comments, you need to log in

3 answer(s)
O
OnYourLips, 2015-10-23
@OnYourLips

> I will be glad to links to resources with Best Practices on this topic)
https://www.python.org/dev/peps/pep-0008/
1. Yes. They are in the context of the method.
2. Yes.
3. Only if it is not clear in the context of the method. Make it clear and avoid such names.

A
anauthentic, 2015-10-24
@anauthentic

well, in general, https://www.python.org/dev/peps/pep-0020/
By the way, on the third point - while participating in one project on eiffel, I managed to get tired of such verbosity. I will only add to the previous post that it is always better to avoid this and compensate with comments

A
abcd0x00, 2015-10-24
@abcd0x00

In general, the code should be written as if you will open it in 10 years. Introduced?
If there are some nuances, you still won’t remember them.
1) Functions must be called imperatively (imperative mood).
2) Use namespaces.

открыть_ёжика_левого
открыть_ёжика_правого

open - this is the first namespace of the
hedgehog - this is the second namespace of the
right - this is the third namespace (empty)
of the left - this is the fourth namespace (empty)
One space is in another, so the names are short and specific.
You are guided by quantity: if there are few actions, and there are many animals, then animals are in the actions; if there are few animals and many actions, then actions are found in animals.
3) Use Idioms Use i
instead - this is item This idiom for iterating over the elements of a sequence is suitable for many languages. Therefore, even a more understandable ticket will not look as familiar as i has been using for years.
4) When creating a name, make it unique
What else could I call this name in this code? If the answer is no, then the name is good.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question