1

In this example, it's working hotels as a class variable no NameError

class Hotel():
    """""""""
    this is hotel class file
    """
    hotels = []
    def __init__(self,number,hotel_name,city,total_number,empty_rooms):
        self.number = number
        self.hotel_name = hotel_name
        self.city = city
        self.total_number = total_number
        self.empty_rooms = empty_rooms

        Hotel.hotels.append([number,hotel_name,city,total_number,empty_rooms])

    def list_hotels_in_city(self,city):
        for i in hotels:
            if city in i:
                print "In ",city,": ",i[1],"hotel, available rooms :",i[4]

In the following example its not working

from twilio.rest import Client


class Notifications():
    customers = []

    def __init__(self,customer_name,number,message):
        self.customer_name = customer_name
        self.number = number
        self.message = message
        Notifications.customers.append([customer_name,number,message])

    def send_text_message(self,customer_name):
        for i in customers:
            print "triggeredb"

inst = Notifications("ahmed","+00000000000","messagesample")
print "instance : ",inst.customers
inst.send_text_message("ahmed")

NameError: global name 'customers' is not defined

Update

for first example nothing called to show error but issue solved for second example Thanks Tom Dalton , scharette and James

7
  • 2
    When you call for i in customers: customers is not in scope of that function. Commented Jun 20, 2018 at 14:28
  • 2
    Try for i in self.customers: Commented Jun 20, 2018 at 14:28
  • @TomDalton Thanks it's working now, but why in the first example I didn't use self.hotels and its working? Commented Jun 20, 2018 at 14:41
  • I don't know - have you defined a variable hotels at the module level? Commented Jun 20, 2018 at 15:32
  • It's not defined anywhere else, its same like both examples they are different files . Commented Jun 20, 2018 at 16:39

2 Answers 2

1

As I said in my comment, When you call for i in customers:, customers is not in scope of that function.

I just wanted to add also, that you use

 Notifications.customers.append([customer_name,number,message])

but you also declare

customers = []

Note that the former is a class variable and will share the variable among Notifications instances. The latter represent an instance variable. If your goal is to have a customers list for every specific object, you should use self.customers.

So basically,

You want shared list between objects ?

for i in Notifications.customers:

You want specific list for every object ?

for i in self.customers:
Sign up to request clarification or add additional context in comments.

6 Comments

can you explain why its working in first example without Hotel.hotels or self.hotels
I prefer not to add this to my answer since it should not work. As @James said when you ran your first example, you had a variable called hotels in your global (interpreter) scope. which is probably the case.
actually you solved it but still I have a question how it's working in the first example but this will not affect the main requirement from my question.
can you try it in your interpreter?
@ahmedyounes, James did look his answer.
|
1

I think it's very likely that when you ran your first example, you had a variable called hotels in your global (interpreter) scope. Thats why it's working. If I copy paste your example into my interpreter it fails with the same error message as your second code sample.

If your send_text_message function only accesses class variables (no instance variables) I would recommend making it a class method like this :

@classmethod
def send_text_message(cls, customer_name):
    for i in cls.customers:
        print "triggeredb"

That way you can access the class variables using the cls variable and won't have to repeat the class name in your function (which is nice, as if you change the class name - you won't have to go hunting through your code for repetitions).

5 Comments

Thanks for your notice but I'm trying to confirm that it will give an error in different global interpreter scope but it's not giving error !!
Check in locals() maybe? Or could you have it defined at the module level?
No it's not defined anywhere else just like the example I used in my question, it's really exhausting getting nervous :(
can you try it in your interpreter?
how can I remove or reset my interpreter, I restarted everything and still have this issue I even made a new class and for hotels its not giving error !!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.