Python Forum
Call a variable - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Call a variable (/thread-15395.html)

Pages: 1 2


Call a variable - jmf08 - Jan-16-2019

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



RE: Call a variable - ichabod801 - Jan-16-2019

If you want to pass a function as an object without calling it, you just remove the parentheses. As in add = cmod.meth22.


RE: Call a variable - jmf08 - Jan-16-2019

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


RE: Call a variable - woooee - Jan-16-2019

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.


RE: Call a variable - jmf08 - Jan-16-2019

'add' from module2 ---> module3


RE: Call a variable - ichabod801 - Jan-16-2019

(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.


RE: Call a variable - jmf08 - Jan-17-2019

Sorry, I just want to use the 'add' variable to my meth31.


RE: Call a variable - ichabod801 - Jan-17-2019

(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?


RE: Call a variable - jmf08 - Jan-17-2019

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.


RE: Call a variable - ichabod801 - Jan-17-2019

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.