0

How to use a global variable for a class object creation?-Python

Python:::Hei everyone . My problem is that i cant use a global variable outside of an other function. I need it though in my example. Here is the code peace im struggeling with #================================================== supermarketname = "test" def supermarket_check_name(): global supermarketname check = True while check: supermarketname_to_check = input("Enter a Supermarket name: ") if len(supermarketname_to_check) >19: print("This name is to long") else: supermarketname = supermarketname_to_check check = False class supermarket_class(): def __init__(self,name,bananas,apples,melones,peaches,coconut): self.name = name self.bananas = bananas self.apples = apples self.melones = melones self.peaches = peaches self.coconut = coconut supermarket = supermarket_class(supermarketname,1,2,3,4,5) print(supermarket.name) #================================================ #=====================Output===================== #when the input pops up and u type in Bobssupermarket it still prints test #=============================================== when i would add a function like : def print_supermarketname(): print(supermarketname) and call it after the supermarket_check_name function it gives me the input as name, but i cant put it in a class. Hope someone can help me. Thannks. z77

25th Mar 2017, 12:10 AM
z77
5 ответов
+ 1
I didn't get your point clearly, however I think your problem is that you are updating the global variable "supermarketname" in the function "supermarket_check_name" after you create the class instance "supermarket". and you didn't update the instance variable "supermarket.name" So if you try to print "supermarket.name" and "supermarketname", you will find that they will have different values. print(supermarket.name, "-", supermarketname) In order to solve that you have either to update the global variable "supermarketname" before creating the "supermarket" class, or update the instance variable "supermarket.name" directly instead of updating the global variable "supermarketname" Wish that was your problem :)
25th Mar 2017, 2:30 PM
Moataz El-Ibiary
Moataz El-Ibiary - avatar
+ 1
You may use a default value, like: def __init__(self, bananas, apples, melones, peaches, coconut, name=""): In this case, you don't have to set the parameter "name" while creating the instance. supermarket = supermarket_class(1,2,3,4,5) But remember, parameters with default value have to be at the end.
26th Mar 2017, 12:41 PM
Moataz El-Ibiary
Moataz El-Ibiary - avatar
0
you got my point. but i dont know how to update it
25th Mar 2017, 2:33 PM
z77
0
and how do i create an object with an missing variable? do i create it and than overwrite it later?
25th Mar 2017, 2:34 PM
z77
0
i found a solutio instead of making a variable in the function, i let the function return the value and than asign it to a variable: def supermarket_check_name(): check = True while check: supermarketname_to_check = input("Enter a Supermarket name: ") if len(supermarketname to check)>19: print("This is too long") else: return supermarketname_to_check supermarketname = supermarket_check_name
26th Mar 2017, 2:12 PM
z77