Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Call a variable
#1
Instead of calling a variable , the whole method is being called. Is there something I need to change to call only the variable 'add'?
I just want to use the variable 'add' to module3. tia


module2.py
    def meth22():
  #      self.add = add
        add = input('What plan do you want to add?\n')
        return add

#add
module3.py
import pickle
from module2 import cmod2
plannerlist = []

class cmod3:
    def meth31():
        planner = []
        add = cmod2.meth22()
        planner.append(add)
        plannerlist.append(planner)
        pickle.dump(plannerlist, open('pl','wb'))
        print('Your plan:')
        for planner in plannerlist:
            print(*planner)
        return  planner
    #return plannerlist

    #      print(pl)
#show
Reply
#2
If you want to pass a function as an object without calling it, you just remove the parentheses. As in add = cmod.meth22.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
What if the 'add' variable only I want to call to module3?
Reply
#4
from module2 import cmod2
There is no cmod2 in the code you posted.

Quote:What if the 'add' variable only I want to call to module3?

There is no module3 in the code you posted so we have no idea if it is in one of the programs you posted or another one entirely.
Reply
#5
'add' from module2 ---> module3
Reply
#6
(Jan-16-2019, 04:41 AM)jmf08 Wrote: What if the 'add' variable only I want to call to module3?

What does this mean? It doesn't make any sense to me. It looks like you are trying to make add a method of cmod2, but you are doing it from a method of cmod3. Perhaps if you could explain what the code is for and what you are trying to do, that would help. All of these repetitive cryptic names in your code make it very hard to understand what is going on and what the goal is.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
Sorry, I just want to use the 'add' variable to my meth31.
Reply
#8
(Jan-17-2019, 01:10 PM)jmf08 Wrote: Sorry, I just want to use the 'add' variable to my meth31.

Again, I have no idea what you mean by that. Looking at your code, meth31 asks the user for a value and returns that value. Then meth32 assigns that value to the variable add, and appends it to the list planner, and then pickles planner. What's the problem? How is that not working?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#9
It repeats the method, it should not. It calls the whole method instead of just the variable 'add'.
So when I run it, its getting the user input twice. It should take only 1 input.
Reply
#10
That's what methods do. When you call the method it runs the whole method. If you want to get the value later, you need to store it:

def get_add(self):
    self.add = input('What plan do you want to add?\n')
    return self.add
The first time you want the value, you use cmod2.get_add(). The second time you want the value, you use cmod2.add.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Simple automated SoapAPI Call with single variable replacement from csv asaxty 1 2,107 Jun-30-2020, 06:38 PM
Last Post: asaxty
  variable call back into an array yamifm0f 3 2,382 Jun-07-2019, 02:44 PM
Last Post: heiner55
  call a variable from one function to another ... evilcode1 4 3,995 Sep-16-2018, 10:50 AM
Last Post: gruntfutuk
  How to call a variable declared in a function in another python script lravikumarvsp 2 2,942 Jun-04-2018, 08:45 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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