1

I haven't found precise answer to the question (Python).

Is a good practice to intialize variables at the beggining or should that be done later? And what type of the value should I give them at the beginning?

example:

variable_str = None  #  Should I give None to them all?
variable_int = None  #  
variable_float = 0.0 # Should I give float for type float?

# Code.......
# I will use that variables later on,....

Some say it is a good practice, but I am confused which data type should be used? Python is dynamically typed, but nevertheless what is the best practice?

3
  • 2
    No, don't do that. Commented Mar 30, 2019 at 23:53
  • Python is dynamically typed so there is no need to declare them with a default value. Just define them when you need them with the appropriate value. You can look at type hints if you want some form of static type checking (e.g. mypy), but this is not enforced by CPython at runtime. Commented Mar 30, 2019 at 23:54
  • I would discourage it. Commented Mar 30, 2019 at 23:56

1 Answer 1

1

There's no need to mention variables before you first need to give them an actual useful value. Don't put dummy values at the top of your program.

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.