In PHP, if I want to know if a variable was started, I use isset
.
How can I do the same in Python?
In PHP, if I want to know if a variable was started, I use isset
.
How can I do the same in Python?
Natively does not.
If the need is to check only if the variable exists I recommend starting all variables with some value, such as None
or False
(depending on the need), you do not have to create something complex for this the examples).
It should be understood that PHP isset
does much more than check if a variable exists , it also checks if a key exists or can even check if a string contains a minimum of catheters, which will allow you to check if the variable exists and check the string at the same time, so I'll tell you right now that just like isset
of PHP does not have in python.
Of course if the intention is to check a variable just you could simply try something like:
try:
minhavar
print('Variavel definida')
except NameError:
print('Variavel não definida')
You could also use globals()
to check the variables defined in the scope and create a function of its own, something like:
def isset(nameVar):
return nameVar in globals()
See more about this in this question:
And the use would be:
if isset('variavel'):
print('Variavel definida')
except NameError:
print('Variavel não definida')
But as I said, isset
of PHP has a very specific behavior, so for every thing of isset
would have to create a "test", examples of PHP functionalities:
//Se a variavel for uma string, verifica se existe e se ela contém 3 caracteres
//o index de uma string começa pelo zero, ou seja o primeiro caractere é o zero, o segundo vai ser o 1 e assim por diante
if (isset($foo[2])) {
...
}
//Checando se uma variavel tem acesso a uma propriedade especifica (o valor não pode ser nulo)
if (isset($foo->bar)) {
...
}
//Checando se uma variavel de array tem acesso a uma chave e esta chave não é um valor nulo
if (isset($foo['bar'])) {
...
}
Note that even if a variable, key, or property of an object exists, isset
will return as false
in PHP, because NULL
is the only exception.
It would be a bit complex behavior to transport to Python, it is not impossible, but it will depend on your need, there is no reason to create a "monstrous" function in the code if you will not use everything, In Python I would choose to define everything, right at the beginning of the script or the scope of a def
set to None
, for example:
foo = None
bar = None
baz = None
and would look like this:
if foo is None:
print('foo é nula')
else:
print('foo foi definida')
You can simplify to:
foo = bar = baz = None
Or you can use unpack , so if you have 3 variables use *3
, as in this example:
foo, bar, baz = (None,)*3
If you want to set as False
:
foo, bar, baz = (False,)*3
This helps to avoid a certain repetition of codes
Built-in functions locals () and < globals () , point to the local and global symbol tables, respectively.
You can verify that the variable
var_name
exists in these dictionaries as follows:
if 'nome_var' in locals():
print('variável local')
or
'nome_var' in globals()
Practical, but less recommended - as a function primarily designed for use in the python interactive environment - would be to use the built-in function dir () , which points to a list of available names in the current scope.
'nome_var' in dir()