Importing Objects from a Class | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Importing Objects from a Class

Hello people good night. I have the following problem: to make it easier to maintain / translate an application, I created a class called ProgramVocab to store all the strings used. However, when I try to import these strings to use them in the project, I get the AttributeError, saying that they are undefined attributes. For example: How the class is: https://hastebin.com/haruhacohe.py To call the objects I am using the following: https://hastebin.com/oxatumuyep.coffeescript (Please ignore the .coffeescript extension that Hastebin generated above) Well, when I try to modify the calling mode of the class - using an instance - as explained in this link ( https://stackoverflow.com/questions/3434581/accessing-a-class-member-variables-in-JUMP_LINK__&&__python__&&__JUMP_LINK ) I get theTypeError: 'module' object is not callable. So, what am I doing wrong? Sounds like a simple problem, but I can not figure it out.

1st Aug 2018, 11:59 PM
rfPeixoto
rfPeixoto - avatar
2 Answers
+ 7
what do you think about this code? #page one.py class ProgramVocab: def __init__(self, text_1, text_2, text_3): self.text_1 = text_1 self.text_2 = text_2 self.text_3 = text_3 #page two.py from page_one import ProgramVocab as vocab #instance variable x = vocab("attack", "defend", "hide") print("Skill 1: ", x.text_1) #attack print("Skill 2: ", x.text_2) #defend print("Skill 3: ", x.text_3) #hide
2nd Aug 2018, 1:05 AM
Mohammed Chami
Mohammed Chami - avatar
+ 2
Mohamed :) It would work perfectly, but I would need to type the texts outside the 'ProgramVocab' file. Would not there be a more practical way to leave all strings in a single file?
2nd Aug 2018, 10:55 AM
rfPeixoto
rfPeixoto - avatar