Python Forum
Passing variable to another function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Passing variable to another function
#1
I couldn't get this variable passed to the next function. This last bit of code is me throwing everything at it. This actually worked but strangely still gave an error message:-
Can anyone see where I'm going wrong?

def dataread():
    test_file = open('Database.txt', 'r')
    test_lines = test_file.readlines()
    test_file.close()
    #Print line
    liner = (test_lines[1])

    colon = liner.find(":")
    semi = liner.find(";")
    comma = liner.find(",")
    length = len(liner)

    name = (liner[colon+1:semi])
    location = (liner[semi+1:comma])
    gps = (liner[comma+1:length])

    htmlwr(location)
    return location


def htmlwr(location):
    print(location)

dataread()
htmlwr(location)
Reply
#2
Try combining the last two lines into
htmlwr(dataread())


I think you are expecting the return to make the variable globally available. I think it is more like it gives the function call the value of what is returned.
Reply
#3
Ahha... k that's working now but its printing out twice. I try and figure it out from here. Thx Michael you're a legend!
Reply
#4
All I'm trying to do here is give the variable more scope without using the global function.
Sorry guys I thought I could solve this. At first I just did as you asked Michael. Removed the 2 function calls and placed the function inside the other. As I mentioned - this caused it to print:

Quote:Mylocation
Mylocation

So I removed the "return"

And I got

Quote:Mylocation
None

After a series of attempts my last one is below
This weirdly prints
Quote:Mylocationfoo
Mylocationfoo
I though it would print
Quote:Mylocationfoo
Mylocation

I thought only the second one was going to have the foo
def dataread():
    test_file = open('Database.txt', 'r')
    test_lines = test_file.readlines()
    test_file.close()
    #Print line
    liner = (test_lines[1])
 
    colon = liner.find(":")
    semi = liner.find(";")
    comma = liner.find(",")
    length = len(liner)
 
    name = (liner[colon+1:semi])
    location = (liner[semi+1:comma])
    gps = (liner[comma+1:length])
 
    htmlwr(location)
    return location
 
 
def htmlwr(location):
   Tester = location + "foo"
   print(tester)
 

htmlwr(dataeread())
If I remove the "return value" from this one it prints
Quote:Mylocationfoo
Error

It's as if the things looping inside itself. Which I'm sure might be a useful trait at some point. But not with this

Lastly I tried to switch the functions
dataeread(htmlwr())
But it gave me a position error
Reply
#5
You were getting the double print because lines 18 and 27 both call htmlwr(). And it is the only print statement and tacks "foo" onto it each time.

dataread(htmlwr()) should have given you
Error:
dataeread() takes 0 positional arguments but 1 were given
That's because it isn't asking for anything, but you gave it one thing.

dataread() is at the global level, so you can access it all the same. I'm not sure what you are specifically trying to do. If you put a print(location) statement inside dataread() you wont't get the "foo".
Reply
#6
@michael1789.
The print is only for testing. Eventually I'm going to pass that location to another function that's going to use it.
All I want is for the variable to have scope in both functions. I could just make it a global variable but everyone keeps saying it's bad to use them. Maybe I should?
Reply
#7
The ultimate answer to your question is OPP(Object Oriented Programming).

Make everything that goes together part of a Class, then you can refer to any part easily and make as many "instances" of the class as you want. You then refer to the instance and can run it's functions(methods) whenever you want, or you can hand the entire class to something else through "inheritance". I'm a newbie at this, but OOP is where it's at. The classes I use most are pygame sprites.

I can't really tell what you program does, but I made a rough example just to show the idea. If you make something like this work then you can read in a process as many different files as you want.
class Data_File():
    def __init__(self, file_name):
        self.test_file = open(file_name, 'r')
        self.test_lines = test_file.readlines()
        self.test_file.close()
        self.liner = (self.test_lines[1])
        self.colon = self.liner.find(":")
        self.semi = self.liner.find(";")
        self.comma = self.liner.find(",")
        self.length = len(self.liner)
        self.name = (self.liner[self.colon+1:self.semi])
        self.location = (self.liner[self.semi+1:self.comma])
        self.gps = (self.liner[self.comma+1:self.length])
  
  
    def htmlwr(self):
        Tester = (self.location, "foo")
        print(Tester)

dat1 = Data_file("Database1.txt")
dat2 = Data_file("Database2.txt")

dat1.htmlwr()
dat2.htmlwr()

print(dat1.liner, dat1.name)
print(dat2.location, dat1.location)

bunch_o_stuff = (dat1.gps, dat1.liner, dat2.length, dat1.name,
                 dat2.liner, dat1.test_lines)
print("Here is a bunch of stuff", bunch_o_stuff)
Reply
#8
Hopefully you guys can see this image. Apoligies about the massive watermark...I need an new cloud I think...
Anyways sorry Michael.
I was very hopeful that this was going to work.
Object oriented programming, sounds good, though perhaps a bit complicated for a big n00been00b like me but I thought well I'll just have to roll with it.
Unfortunately my first experience hasn't been great :(

Now what I did was to rename Database.txt to Database1.txt
Then I copied that file as Database2.txt

When I run the script, as you see, its giving: -

Quote:
Traceback (most recent call last):
File "/home/pi/Desktop/Pyread.py", line 47, in <module>
dat1 = Data_file("Database1.txt")
NameError: name 'Data_file' is not defined


And I definitely don't have the skills to troubleshoot this. Though I did try to replace Database1.txt with the full path name instead. Same error

I discovered debuggers in my attempt to solve this... when I run the debugger in Thorny, it seems to me that the class exists.
When the debugger gets to that line it shows
Quote:Name_________________Value
Data_File _____________<class'__main__.Data_File'>

[Image: rpi.jpg]
Reply
#9
The name error is a typo (capitalization) I had in there. The class is named "Data_File", not "Data_file". And yes, you do have the skill to find stuff like that. Coding is frustrating.

It might help you to watch some youtube videos on python classes just so you have a feel for the concept. As I said, it's the answer to your problem and it is what people mean when they say don't use global variables.
Reply
#10
Basics of Classes -https://www.youtube.com/watch?v=apACNr7DC_s&t=78s
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable for the value element in the index function?? Learner1 8 919 Jan-20-2024, 09:20 PM
Last Post: Learner1
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 750 Nov-23-2023, 02:53 PM
Last Post: rob101
  Printing the variable from defined function jws 7 1,644 Sep-03-2023, 03:22 PM
Last Post: deanhystad
  Function parameter not writing to variable Karp 5 1,171 Aug-07-2023, 05:58 PM
Last Post: Karp
  passing dictionary to the function mark588 2 1,102 Dec-19-2022, 07:28 PM
Last Post: deanhystad
  Retrieve variable from function labgoggles 2 1,147 Jul-01-2022, 07:23 PM
Last Post: labgoggles
  Cant transfer a variable onto another function KEIKAS 5 2,048 Feb-09-2022, 10:17 PM
Last Post: deanhystad
  passing php variable to python file jerald 1 2,860 Jul-07-2021, 11:46 AM
Last Post: Larz60+
  Passing flags to python script, through a function xbit 4 4,187 Apr-20-2021, 06:32 AM
Last Post: ndc85430
  Please explain uncommon way of declaring and using variable [function.variable] esphi 4 2,516 Nov-07-2020, 08:59 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020