Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Call a variable
#11
(Jan-17-2019, 03:11 PM)ichabod801 Wrote: The first time you want the value, you use cmod2.get_add(). The second time you want the value, you use cmod2.add.



myobject4 = cmod2.add()
Error:
AttributeError: type object 'cmod2' has no attribute 'add'
Reply
#12
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
Reply
#13
    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
Reply
#14
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
Reply
#15
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.

Change line 10 to add = cmod2.meth22().

I just tried this and gives
Error:
TypeError: meth22() missing 1 required positional argument: 'self'
Reply
#16
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
Reply
#17
Should I add object to my classes?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Simple automated SoapAPI Call with single variable replacement from csv asaxty 1 2,106 Jun-30-2020, 06:38 PM
Last Post: asaxty
  variable call back into an array yamifm0f 3 2,380 Jun-07-2019, 02:44 PM
Last Post: heiner55
  call a variable from one function to another ... evilcode1 4 3,994 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,941 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