iamnewuser February 2016

Name not definef error when using Class

I am facing issue with the Code which is framed with Class.

Basically before It was simply a file with functions defined in it.

So When I was trying to execute the file using command python filename.py, It is working fine as needed.

Code sample is as follows:

# Getting the tenant list 
# Fetch the creation_date of tenant if exists
def get_tenants():

    # Fetch tenant list
    tenants_list = keystone.tenants.list()

    # Fetch tenant ID
    for tenant in tenants_list:
        tenant_id = tenant.id
.
.
.

get_tenants()

So as shown in aboce code in the file I am trying to call get_tenants function, Also it is working fine as needed with no error.

Now I have Created the Class then moved all the functions in to the same.

Above function is Rewritten as follows now.

def get_tenants(self):

        # Fetch tenant list
        tenants_list = keystone.tenants.list()

        # Fetch tenant ID
        for tenant in tenants_list:
            tenant_id = tenant.id

Then I have called the Function as follows:

billing = BillingEngine()
billing.get_tenants()

But, now I am getting the error as follows:

root@devstack:/opt/open-stack-tools/billing# python new_class.py
Traceback (most recent call last):
  File "new_class.py", line 281, in <module>
    BillingEngine().get_tenants()
  File "new_class.py", line 75, in get_tenants
    tenants_list = keystone.tenants.list()
NameError: global name 'keystone' is not defined

Note: Will provide the full file if needed.

Answers


Maxim Panfilov February 2016

May be you must define this?

class Example(object):

   keystone = Keystone()

   def get_tenants(self):
       self.keystone.do_something()

Post Status

Asked in February 2016
Viewed 3,110 times
Voted 14
Answered 1 times

Search




Leave an answer