Regarding not declaring variables in Python...

by RpgFan (Login rpgfan3233)
R

If you're referring to:

x = 2 #OR
y = 'FooBar'

then, you're correct in noting that you don't need to declare them before giving them values. However, with regards to the following, the variable 'n' must have a value for the statement to execute at all:

print(n if n is None else 'Bummer')

Even JavaScript has gotten more strict about things. If a variable is not defined before its value is retrieved, an error/exception/whatever is raised. Note that this behaviour is only caused by not having a value for the variable when its non-existent getter function is called (after all, how can you retrieve something if it doesn't exist!?). You can assign a value such as undefined (JavaScript) or None (Python) just fine. Then things will work.

QB seems rather unique in the fact that you can just use the following readily:
IF n = 0 THEN PRINT n ELSE PRINT "Bummer"

That is perfectly acceptable in QB, which is very strange compared to many other languages I've used to create programs.



    
This message has been edited by rpgfan3233 on Apr 21, 2009 9:18 PM
This message has been edited by rpgfan3233 on Apr 21, 2009 9:14 PM

Posted on Apr 21, 2009, 9:12 PM

Respond to this message   

Return to Index


Response TitleAuthor and Date
I can think of some languages that let you use variables uninitializedDean Menezes on Apr 22
 * What does this have to do with QB64? on Apr 22
  It is a response to rpgfan's post about python not allowing uninitialized variablesqbguy on Apr 22
   * Why would you want it to? on Apr 22