Python Forum
Use variable from one function in another without return
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Use variable from one function in another without return
#1
I'm having those two functions in my code and want to be able to use the variable tdate from the function def todos_new(): in my second function def todos_print():.

def todos_new():
    tid = 139283983
    ttext = "text"
    tdate = "2018-12-12"
    new = tclass.ToDo(tid, ttext, tdate)
    todos.append(new)

def todos_print():
    datesearch = input('Search Date YYYY-MM-DD:')
    if datesearch < todos(tdate):#NameError: name 'tdate' is not defined
        print(todos)
Any idea how I can use that variable in my second function without return in my first function?
Thanks for your input!
Reply
#2
Either use global variable or use class.
Reply
#3
Sorry, didn't post the code from the class before - I use the following:

class.py
class tclass:
    def __init__(self, tid, ttext, tdate):
            self.tid = tid
            self.ttext = ttext
            self.tdate = tdate
main.py
import tclass
todos =[]

def todos_new():
    tid = 139283983
    ttext = "text"
    tdate = "2018-12-12"
    new = tclass.ToDo(tid, ttext, tdate)
    todos.append(new)

def todos_print():
    datesearch = input('Search Date YYYY-MM-DD:')
    if datesearch < todos(tdate):
        print(todos)
Still get the NameError: name 'tdate' is not defined. Any idea? Thanks!
Reply
#4
To implement these with the class, the functions need to be methods of the class and todos_print() will need to use self.tdate. From what I'm seeing, it appears that you intend to make a list of todo items. In that case, you should have a second class to contain and manage that list. Something like this:

class Todo_list:
    todos = []

    def add_todo_item(self, todo):
        if isinstance(todo, tclass):
            self.todos.append(todo)

    def print_todo_list(self):
        print(self.todos)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable for the value element in the index function?? Learner1 8 546 Jan-20-2024, 09:20 PM
Last Post: Learner1
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 514 Nov-23-2023, 02:53 PM
Last Post: rob101
  nested function return MHGhonaim 2 562 Oct-02-2023, 09:21 AM
Last Post: deanhystad
  Printing the variable from defined function jws 7 1,165 Sep-03-2023, 03:22 PM
Last Post: deanhystad
  Function parameter not writing to variable Karp 5 892 Aug-07-2023, 05:58 PM
Last Post: Karp
  return next item each time a function is executed User3000 19 2,171 Aug-06-2023, 02:29 PM
Last Post: deanhystad
  function return boolean based on GPIO pin reading caslor 2 1,131 Feb-04-2023, 12:30 PM
Last Post: caslor
  Retrieve variable from function labgoggles 2 999 Jul-01-2022, 07:23 PM
Last Post: labgoggles
  Cant transfer a variable onto another function KEIKAS 5 1,835 Feb-09-2022, 10:17 PM
Last Post: deanhystad
  return vs. print in nested function example Mark17 4 1,674 Jan-04-2022, 06:02 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

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