showMonitors() – what does "None" mean?

Asked by Rainer

Just used the showMonitors() command on one of my systems. It returned

*** monitor configuration [ 2 Screen(s)] ***
*** Primary is Screen 0
Screen 0: R[0,0 1680x1050]@S(0)
Screen 1: R[-1680,0 1680x1050]@S(1)
*** end monitor configuration ***
None

Everything is correct (very nice info BTW, thank you), I was just wondering what the "None" in the last line is referring to?

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
RaiMan
Solved:
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

testing with 1.1.4:
a script containing only

showMonitors()

does not produce a None after *** end monitor configuration ***

Revision history for this message
Mike (maestro+++) said :
#2

In Windows 10 showMonitors() does not produce 'none'. However the function does have a return value of None.

I couldn't find this in the documentation by the way.

Revision history for this message
Rainer (formulator) said :
#3

Just did some further testing.

As RaiMan already noted, showMonitors() by itself does not produce the "None".
The "None" in my case resulted from calling showMonitors() from within a print() function.

The strange behavior here seems to be that the print() function in general does not print a "None" (or whatever) return value.

This is independent of the "content" that shall be printed:

The following code:

print("WTF?")

… just prints the text and no subsequent "None".

The following code:

def func():
    return "WTF?"
print(func())

… just prints the text and no "None".

The following code (executing a print() in the called function:

def func():
    print("WTF?")
func()

… also just prints the text.

But the following code:

def func():
    print("WTF?")
print(func())

… prints the text plus a subsequent "None". Obviously the "inner" print() function causes the called func() function to return "None", which is then printed from the "outer" print().

To check that: The following code:

def func():
    print("WTF?")
    return 2
print(func())

… prints the text and then the "2".

The following code results in an error:

def func():
    result = print("WTF?")
    return result
printfunc())

… [error] script [ Untitled ] stopped with error in line 2 at column 13
… [error] SyntaxError ( "no viable alternative at input 'print'", )

print() seems to provide a return value, which cannot be assigned, but is returned anyway to a calling "outer" function …

Astonishingly (or not?) the following code:

result = showMonitors()
print(result)

… prints the Monitor configuration PLUS the "None"!

Even if a put a str() in between to "isolate" the result varable from the fact that the assigned value comes from a function:

result = str(showMonitors())
print(result)

… I get the additional "None" …

The following code:

result = "WTF?"
print(result)

… just prints the text, as expected.

Not sure whether this is really consistent … why does the print() function think that the result variable contains the output of showMonitors() if already the output of this function is explicitly converted to a string before being assigned to the result variable in the first place? And there is no called function involved and no previous print() in the code that could result in "None" being returned to wherever …

BTW: I tried all printing with "print" as a "classic" Python 2 statement and as a Python 3 function, this had no influence.

All this is of course not really important, just somewhat fascinating …

Revision history for this message
Best RaiMan (raimund-hocke) said :
#4

@Mike
showMonitors() is a function at the Java level having this signature:
  public static void showMonitors()

hence it does not return anything.

The message output is done inside the function.

If you assign the not existing return value (void) of showMonitors() to a variable, then in the Python context this variable is defined and does not have a value, which is signalled by None. That is Python ;-)

Revision history for this message
Rainer (formulator) said :
#5

Thank you, RaiMan, that explains it.

In my example from above

result = showMonitors()

the monitor configuration is printed by the showMonitor function, and the result ("None") is assigned to the result variable.

Could have figured that out myself …

Revision history for this message
Rainer (formulator) said :
#6

Thanks RaiMan, that solved my question.