About 58,900 results
Open links in new tab
  1. Python Variable Declaration - Stack Overflow

    Jun 13, 2012 · Variables in Python can hold values of any type, and you can't restrict that. Your question specifically asks about classes, objects and instance variables though. The idiomatic way to create instance variables is in the __init__ method and nowhere else — while you could create new instance variables in other methods, or even in unrelated ...

  2. python - How can I use a global variable in a function? - Stack …

    Jul 11, 2016 · Since local variables are more common than global variables in any serious and non-trivial system, Python's system makes more sense in most cases. You could have a language which attempted to guess, using a global variable if it existed or creating a local variable if it didn't.

  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. How to create module-wide variables in Python? [duplicate]

    Dec 30, 2009 · However, let's suppose you want to use one of your module-scope variables as a global inside a function, as in your example. Python's default is to assume that function variables are local. You simply add a global declaration in your function, before you try to use the global. def initDB(name): global __DBNAME__ # add this line!

  7. python - How do you create different variable names while in a …

    Using dictionaries should be right way to keep the variables and associated values, and you may use this: dict_ = {} for i in range(9): dict_['string%s' % i] = 'Hello' But if you want to add the variables to the local variables you can use:

  8. How to set environment variables in Python? - Stack Overflow

    os.environ behaves like a python dictionary, so all the common dictionary operations can be performed. In addition to the get and set operations mentioned in the other answers, we can also simply check if a key exists. The keys and values should be stored as strings. Python 3. For python 3, dictionaries use the in keyword instead of has_key

  9. python - Variables declared outside function - Stack Overflow

    In Python, variables that are only referenced inside a function are implicitly global. If a variable is assigned a value anywhere within the function’s body, it’s assumed to be local unless explicitly declared as global.

  10. python - Class (static) variables and methods - Stack Overflow

    Sep 16, 2008 · Flipping the switch into "opinion", I think a lot of the times, static methods in C#/Java were made because the languages took a hard line "no functions" stance, in C#/Java you can only have methods (i.e., a function that's part of a class), Python doesn't have this restriction (which is for the best, in my opinion).

Refresh