Is it possible only to declare a variable without assigning any value in Python?

后端 未结 14 1848
暖寄归人
暖寄归人 2020-11-30 17:26

Is it possible to declare a variable in Python, like so?:

var

so that it initialized to None? It seems like Python allows this, but as soon

相关标签:
14条回答
  • 2020-11-30 17:51

    It is a good question and unfortunately bad answers as var = None is already assigning a value, and if your script runs multiple times it is overwritten with None every time.

    It is not the same as defining without assignment. I am still trying to figure out how to bypass this issue.

    0 讨论(0)
  • 2020-11-30 17:51

    Is it possible to declare a variable in Python (var=None):

    def decl_var(var=None):
    if var is None:
        var = []
    var.append(1)
    return var
    
    0 讨论(0)
  • 2020-11-30 17:52

    I'd heartily recommend that you read Other languages have "variables" (I added it as a related link) – in two minutes you'll know that Python has "names", not "variables".

    val = None
    # ...
    if val is None:
       val = any_object
    
    0 讨论(0)
  • 2020-11-30 17:52

    You can trick an interpreter with this ugly oneliner if None: var = None It do nothing else but adding a variable var to local variable dictionary, not initializing it. Interpreter will throw the UnboundLocalError exception if you try to use this variable in a function afterwards. This would works for very ancient python versions too. Not simple, nor beautiful, but don't expect much from python.

    0 讨论(0)
  • 2020-11-30 17:56

    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:

    #note how I don't do *anything* with value here
    #we can just start using it right inside the loop
    
    for index in sequence:
       if conditionMet:
           value = index
           break
    
    try:
        doSomething(value)
    except NameError:
        print "Didn't find anything"
    

    It's a little difficult to tell if that's really the right style to use from such a short code example, but it is a more "Pythonic" way to work.

    EDIT: below is comment by JFS (posted here to show the code)

    Unrelated to the OP's question but the above code can be rewritten as:

    for item in sequence:
        if some_condition(item): 
           found = True
           break
    else: # no break or len(sequence) == 0
        found = False
    
    if found:
       do_something(item)
    

    NOTE: if some_condition() raises an exception then found is unbound.
    NOTE: if len(sequence) == 0 then item is unbound.

    The above code is not advisable. Its purpose is to illustrate how local variables work, namely whether "variable" is "defined" could be determined only at runtime in this case. Preferable way:

    for item in sequence:
        if some_condition(item):
           do_something(item)
           break
    

    Or

    found = False
    for item in sequence:
        if some_condition(item):
           found = True
           break
    
    if found:
       do_something(item)
    
    0 讨论(0)
  • 2020-11-30 17:57

    In Python 3.6+ you could use Variable Annotations for this:

    https://www.python.org/dev/peps/pep-0526/#abstract

    PEP 484 introduced type hints, a.k.a. type annotations. While its main focus was function annotations, it also introduced the notion of type comments to annotate variables:

    # 'captain' is a string (Note: initial value is a problem)
    captain = ...  # type: str
    

    PEP 526 aims at adding syntax to Python for annotating the types of variables (including class variables and instance variables), instead of expressing them through comments:

    captain: str  # Note: no initial value!
    

    It seems to be more directly in line with what you were asking "Is it possible only to declare a variable without assigning any value in Python?"

    0 讨论(0)
提交回复
热议问题