0

#from file mod.dbconnect.py

def dbtest():
    print("The database health is good.")

def dbconnect():
    print("The database is connected.")
  
def dbdisconnect():
    print("The database has been disconnected.")
  
def dbversion():
    print("The database version is 14.0.5.")

####################################
#The file calling it

import mod_dbconnect as a

def db():
    a.dbtest()
    a.dbconnect()
    a.dbdisconnect()
    a.dbversion()
var1 = db()

#The output on console
The database health is good.
The database is connected.
The database has been disconnected.
The database version is 14.0.5.

I thought variables were supposed to be stored in RAM and called as and when needed not act as a function in themselves? How to I store data from a function in a global variable and not have it auto output to screen?

Michael
  • 84
  • 4
  • 2
    There are no variables in your program. – bereal Apr 09 '22 at 20:25
  • 3
    Are you asking why calling `print` prints output to the terminal? – Brian61354270 Apr 09 '22 at 20:26
  • asking why 'var1 = db()' is printing to the terminal – Michael Apr 09 '22 at 20:28
  • 1
    `db()` is calling the function `db`. What you do after that (namely, assign to `var1`) doesn't change the fact that the function was already called. – Silvio Mayolo Apr 09 '22 at 20:28
  • "I thought variables were supposed to be stored in RAM and called as and when needed not act as a function in themselves?" Nothing about this sentence makes sense. For one thing, I don't know what you think `call` means, but variables are not called. More importantly, though, nothing about this code is surprising, and nothing about how it works involves "variables acting like functions" - because they don't. – Karl Knechtel Apr 09 '22 at 20:29
  • The reason you see output is because the `print` function was called, which happens because the functions in `a` were called, which happens because `db` was called. All of those are *functions*. I guess you are expecting that `var1 = db()` somehow *prevents* `db` from being called until `var1` "is needed" (I have no idea what you think that means). The opposite is true: `db` **must** be called immediately, so that it can be known what value to assign to `var1`. It is the same as an arithmetic expression: `a = 3 * 4` makes `3 * 4` happen **right now**, so that `a` can be given the value `12`. – Karl Knechtel Apr 09 '22 at 20:29
  • "How to I store data from a function in a global variable and not have it auto output to screen?" By making a function that actually `return`s data to store, and which doesn't itself do the outputting. – Karl Knechtel Apr 09 '22 at 20:30
  • Those four functions all contain `print()` function calls. Are you seriously asking why they print something? – John Gordon Apr 09 '22 at 20:30
  • @JohnGordon in 3.x, `print` is a function, not a keyword. While it is technically correct that an expression by itself is a kind of statement in the grammar, that elides important details. – Karl Knechtel Apr 09 '22 at 20:31
  • @KarlKnechtel true enough, but in this case that really feels like splitting hairs. Anyway, I edited my comment. – John Gordon Apr 09 '22 at 20:32
  • "db must be called immediately, so that it can be known what value to assign to var1." Ok. I thought this would happen in the background but that answers my question and the functions contain print statements so that makes sense that it would print to screen. – Michael Apr 09 '22 at 20:35
  • Nice you figured it out. Another way to see think about is the following: When you see a something like `foo()` in a statement, then in this line, the code inside the function named `foo` is being executed, **before** the rest of the line is parsed. (well, except when you see the `def foo():` line which defines the function, and it does not execute it.) So `var1=db()` does two things: 1) It runs the code inside function `db()`. 2) it stores the return value of `db()` in the variable `var1`. From 1), the function code calls the module's function from printing – kyriakosSt Apr 09 '22 at 21:19
  • Note that in my above comment, when considering case 2), the function `db()` does not `return` anything, so `var1` will have the value `None`. – kyriakosSt Apr 09 '22 at 21:21

0 Answers0