HuLu ViCa
HuLu ViCa

Reputation: 5452

How to reference an instance variable in Python

I defined an instance variable in the init() method of a class and derived a new class from it. When I try to reference this variable from an instance of the secondary class, it seems it was not inherited.

This is the code:

company=Company()
person=Person()
task=Task()

print(company.get_attrs())

class Entity(Persistent):
    list_of_attrs=[]
    list_of_attrs_flag=False

    def __init__(self):
        attributes={}
        attributes_edition_flag={}
        if not type(self).list_of_attrs_flag:
            type(self).list_of_attrs=self.get_attrs_from_persistent()
            type(self).list_of_attrs_flag=True
            for attr in type(self).list_of_attrs:
                attributes[attr]=''
                attributes_edition_flag[attr]='false'

    def get_attrs(self):
        return(self.attributes)

class Company(Entity):
    def hello(self):
        print('hello')

This is the error I get:

MacBook-Pro-de-Hugo:Attractora hvillalobos$ virtual/bin/python3 control.py
Traceback (most recent call last):
  File "control.py", line 7, in <module>
    print(company.get_attrs())
  File "/Users/hvillalobos/Dropbox/Code/Attractora/model.py", line 48, in get_attrs
    return(self.attributes)
AttributeError: 'Company' object has no attribute 'attributes'

It was working but I moved something (I guess), but I can't find what. Thanks for your help

Upvotes: 0

Views: 63

Answers (1)

Ned Batchelder
Ned Batchelder

Reputation: 375594

In your __init__ method, you have to use self.attributes, not attributes.

Upvotes: 5

Related Questions