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


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

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



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

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.


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

    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



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

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.


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

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'



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

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.


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

Should I add object to my classes?