Jan-17-2019, 04:16 PM
Call a variable
Call a variable
|
Jan-17-2019, 04:30 PM
First of all, you would not call it. You would just reference it:
cmod2.add . No parentheses.Second of all, I need to see how you implemented it. Did you call get_add first, to get the add value from the user? Show me your current code.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness. Recommended Tutorials: BBCode, functions, classes, text adventures
Jan-17-2019, 04:39 PM
def meth22(self): self.add = input('What plan do you want to add?\n') return self.add from module2 import cmod2 plannerlist = [] def meth31(): planner = [] # myobject4 = cmod2.meth22() # myobject4.add() # cmod2.meth22() add = cmod2.add planner.append(add) plannerlist.append(planner) pickle.dump(plannerlist, open('pl','wb')) print('Your plan:') for planner in plannerlist: print(*planner) return planner #show
Jan-17-2019, 04:44 PM
Well, you are never calling meth22, so the add attribute of cmod2 never gets set.
Change line 10 to add = cmod2.meth22() . Then if you need to use add in another method besides meth31, just use add = cmod.add .
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness. Recommended Tutorials: BBCode, functions, classes, text adventures
It worked! in this code but im getting a '<module2.cmod2 object at 0x0000000002918208>' whenever i try to show it.
from module2 import cmod2 plannerlist = [] def meth31(): planner = [] add = cmod2() add.meth22() # myobject4.add() # cmod2.meth22() # add = cmod2.add() planner.append(add) plannerlist.append(planner) pickle.dump(plannerlist, open('pl','wb')) print('Your plan:') for planner in plannerlist: print(*planner) return planner #show (Jan-17-2019, 04:44 PM)ichabod801 Wrote: Well, you are never calling meth22, so the add attribute of cmod2 never gets set. I just tried this and gives
Jan-17-2019, 05:57 PM
I'm beginning to see now. You never instantiate your classes. When you want to persist data, you pickle it and then unpickle it again later. You are not using classes anything like they were intended to be used. It is no wonder that you are running into problems.
If you don't instatiate your classes, you can't save data in instance attributes using self. The only way to store data would be to make meth22 a class method, with a cls paramter instead of a self parameter, and then store it as a cls attribute. But that's just extra effort for no real gain. You really need to go through a tutorial on classes, and you need to get a better understanding of the object-oriented programming paradigm. Then you need to rewrite your code from the ground up.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness. Recommended Tutorials: BBCode, functions, classes, text adventures
Jan-18-2019, 05:50 AM
Should I add object to my classes?
|
|
Possibly Related Threads… | |||||
Thread | Author | Replies | Views | Last Post | |
not able to call the variable inside the if/elif function | mareeswaran | 3 | 505 |
Feb-09-2025, 04:27 PM Last Post: mareeswaran |
|
Variable Substitution call keys | Bobbee | 15 | 2,611 |
Aug-28-2024, 01:52 PM Last Post: Bobbee |
|
Simple automated SoapAPI Call with single variable replacement from csv | asaxty | 1 | 2,711 |
Jun-30-2020, 06:38 PM Last Post: asaxty |
|
variable call back into an array | yamifm0f | 3 | 3,086 |
Jun-07-2019, 02:44 PM Last Post: heiner55 |
|
call a variable from one function to another ... | evilcode1 | 4 | 4,900 |
Sep-16-2018, 10:50 AM Last Post: gruntfutuk |
|
How to call a variable declared in a function in another python script | lravikumarvsp | 2 | 3,598 |
Jun-04-2018, 08:45 AM Last Post: Larz60+ |
Users browsing this thread: 1 Guest(s)