简体   繁体   中英

def function returning wrong value (python 2.7)

So Im working with def functions for the first time and the documentation looked simple enough. and got a few examples. However I cant relate it back to my code. I have 2 files langpy and test1:

test1.py

import time
import thread
import os
import sys
import ctypes
import wmi
from langpy import lang
from langpy import port
string = ""

class service_test:
        def __init__(self):
                thread.start_new(self.do_something, tuple())
                while True:
                        if getattr(sys,'stopservice', False):
                                sys.exit()
                        time.sleep(0.3)

        def do_something(self):
                print(str(lang))
                print(str(port))
                while True:
                        fname = 'c:\\\\test.txt'
                        f = open(fname, 'a')
                        f.write(str(time.time()))
                        #f.write("%s /n/r") % string
                        f.close()
                        time.sleep(1)

if __name__ == "__main__":
        tst = service_test()

langpy:

import os
import time
import ctypes
import wmi

def lang(self, language):
        windll = ctypes.windll.kernel32
        language = str(windll.GetUserDefaultUILanguage())
        return

def port(self, Ardport):
        Ardport = "not found"
        c = wmi.WMI()
        wql = "Select * From Win32_USBControllerDevice"
        for item in c.query(wql):
                        s = str(item.Dependent.Caption)
                        strfind = "Raptor"
                        if (s.find(strfind) >= 0):
                                Ardport = str(item.Dependent.Caption)
        return

I want it to return the strings from Ardport = str(item.Dependent.Caption) and language = str(windll.GetUserDefaultUILanguage()) in the test1 code, but it keeps printing

function lang at 0x027479F0
function port at 0x027503B0

(they have <> on the outside as well but forum format wont include them)

I have an unedited version of langpy (without defining anything) which retunes the correct str

import os
import time
import ctypes
import wmi
windll = ctypes.windll.kernel32
lang = str(windll.GetUserDefaultUILanguage())
print(lang)
Ardport = "not found"
c = wmi.WMI()
wql = "Select * From Win32_USBControllerDevice"
for item in c.query(wql):
        s = str(item.Dependent.Caption)
        strfind = "Raptor"
        if (s.find(strfind) >= 0):
                Ardport = str(item.Dependent.Caption)
print (Ardport)

You are importing these functions correctly:

from langpy import lang
from langpy import port

However, you are printing the reference to these funtions, not the return value.

print(str(lang))
print(str(port))

You want to actually call the funtion, then print what gets returned. You can do this like so:

print(str(lang()))
print(str(port()))

Just one more set of parenthesis!

EDIT: Upon looking further, you also are not returning anything from your functions. Try:

def lang():
            windll = ctypes.windll.kernel32
            language = str(windll.GetUserDefaultUILanguage())
            return language

def port():
        Ardport = "not found"
        c = wmi.WMI()
        wql = "Select * From Win32_USBControllerDevice"
        for item in c.query(wql):
            s = str(item.Dependent.Caption)
            strfind = "Raptor"
            if (s.find(strfind) >= 0):
                Ardport = str(item.Dependent.Caption)
        return Ardport

Note: you will not need argumens for these functions, unless there is some functionality I'm missing.

The self argument is used for class methods, so they know which object to refer to.

You need to return the actual value that you want to print from your function. Replace the existing return lines in langpy with:

return language

and

return Ardport

Then you need to print(lang(my_lang)) and print(port(my_port)) in your test1 file. These print statements will then basically read "print whatever is returned by the function call lang(my_lang) (or port(my_port) )".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM