Posts: 12
Threads: 2
Joined: Jan 2019
Jan-16-2019, 03:48 AM
(This post was last modified: Jan-17-2019, 01:43 PM by jmf08.)
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
Posts: 4,220
Threads: 97
Joined: Sep 2016
If you want to pass a function as an object without calling it, you just remove the parentheses. As in add = cmod.meth22 .
Posts: 12
Threads: 2
Joined: Jan 2019
What if the 'add' variable only I want to call to module3?
Posts: 536
Threads: 0
Joined: Feb 2018
Jan-16-2019, 10:07 AM
(This post was last modified: Jan-16-2019, 10:08 AM by woooee.)
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.
Posts: 12
Threads: 2
Joined: Jan 2019
'add' from module2 ---> module3
Posts: 4,220
Threads: 97
Joined: Sep 2016
(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.
Posts: 12
Threads: 2
Joined: Jan 2019
Sorry, I just want to use the 'add' variable to my meth31.
Posts: 4,220
Threads: 97
Joined: Sep 2016
(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?
Posts: 12
Threads: 2
Joined: Jan 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.
Posts: 4,220
Threads: 97
Joined: Sep 2016
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.
|