About 34,300 results
Open links in new tab
  1. "Initializing" variables in python? - Stack Overflow

    Jun 16, 2015 · In python, if the left hand side of the assignment operator has multiple variables, python would try to iterate the right hand side that many times and assign each iterated value to each variable sequentially. The variables grade_1, grade_2, grade_3, average need three 0.0 values to assign to each variable. You may need something like -

  2. Python best practices: initializing variables - Stack Overflow

    Jul 31, 2018 · Python best practices: initializing variables. Ask Question Asked 6 years, 8 months ago. Modified 6 years ...

  3. python - More elegant way of declaring multiple variables at the …

    Apparently Python 2.7 has dictionary comprehensions, which would make for an extremely concise way to implement that function. This is left as an exercise to the reader, since I don't have Python 2.7 installed :) You can also combine some functions from the ever-versatile itertools module. As they say, There's More Than One Way To Do It. Wait ...

  4. Is it possible only to declare a variable without assigning any value ...

    Mar 20, 2009 · I'm not sure what you're trying to do. Python is a very dynamic language; you don't usually need to declare variables until you're actually going to assign to or use them. I think what you want to do is just. foo = None which will assign the value None to the variable foo. EDIT: What you really seem to want to do is just this:

  5. correct way to define class variables in Python - Stack Overflow

    difference between variables inside and outside of __init__() (class and instance attributes) (12 answers) Closed 4 years ago . I noticed that in Python, people initialize their class attributes in two different ways.

  6. Python initialize multiple variables to the same initial value

    Oct 25, 2015 · Python assigning multiple variables to same value? list behavior concerned with tuples, I want just variables may be a string, integer or dictionary; More elegant way of declaring multiple variables at the same time The question has something I want to ask, but the accepted answer is much complex; so what I'm trying to achieve,

  7. Python Variable Declaration - Stack Overflow

    Jun 13, 2012 · There's no need to declare new variables in Python. If we're talking about variables in functions or modules, no declaration is needed. Just assign a value to a name where you need it: mymagic = "Magic". Variables in Python can hold values of any type, and you can't restrict that. Your question specifically asks about classes, objects and ...

  8. python - Automatically initialize instance variables ... - Stack …

    For Python 3.7+ you can use a Data Class, which is a very pythonic and maintainable way to do what you want. It allows you to define fields for your class, which are your automatically initialized instance variables. It would look something like that: @dataclass class Process: PID: int …

  9. Should all member variables be initialized in __init__

    The reason I do not like the idea of initializing variables to arbitrary defaults is this can be confusing (because it is arbitrary) and is line noise. If the value is not arbitrary and simply a default value that can be changed you should be using a default …

  10. python - Recommended way to initialize variable in if block - Stack ...

    Jan 19, 2012 · In Python there's no great advantage to initializing before a conditional as in your first example. You just need to be sure that the variable is initialized before it's returned. That assumes (based on your examples) that you're using the "single exit point" paradigm.

Refresh