Python Forum
Problem With Scope Of A Variable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem With Scope Of A Variable
#1
I keep getting an error 
Quote:if ((YesAvailable == "True") and (len(str(FullNameEntry.get())) > 3) and (

NameError: name 'YesAvailable' is not defined

When I execute this script, 
What I want here is 
If the SearchingForSpace() function has been called before FinallyReserveParkingBySubmitButton()
It should change the value of the globally declared variable (YesAvailable) to "YES"
Otherwise, 
YesAvailable Should stay as "NO"


ParkingPlaceInfo= "0"
YesAvailable = "NO"

def SearchingForSpace():

   DestinationName = VariableForDestination.get()
   CarType = VariableForCars.get()
   for i in PlacesWithParkingSpaces:
       if (DestinationName == i):
           for j in PlacesWithParkingSpaces[i]:
               if (CarType == j):
                   for k in PlacesWithParkingSpaces[i][j]:
                       if PlacesWithParkingSpaces[i][j][k] == 1:
                           BookedOrNotBooked.config(text="Yes! Place Is Available")
                           global YesAvailable
                           YesAvailable="YES"
                           break
                       else:
                           BookedOrNotBooked.config(text="Pre-Booked.Choose Another Destination\Car Make")

def FinallyReservingParkingBySubmitButton():
   global YesAvailable
   if ((YesAvailable == "YES") and (len(str(FullNameEntry.get())) > 3) and (
   len(str(CarLicensePlateNoEntry.get())) > 3) and (len(str(EmailAddressEntry.get())) > 3) and (
   len(str(PhoneNumberEntry.get())) > 3) and (len(str(DateEntry.get())) > 3) and (len(str(NoOfParkingSpaces.get())) >= 1)):
       result = messagebox.askquestion("Book Now",
                                       "Are You Sure You Want To Book This?(Once Booked, It can not be cancelled)",
                                       icon='warning')
Reply
#2
I think you should do it with a getter - setter:

Quote:class MyClasss():
   def __init__(self):      
       self.__YesAvailable = ''

   def setYesAvailable (self, new_value):
       self.__YesAvailable = new_value

   def getYesAvailable (self,):
       return self.__YesAvailable


  
   def
SearchingForSpace():

       YesAvailable = MyClasss.getYesAvailable(self)
       if ((YesAvailable == "True") and (len(str(FullNameEntry.get())) > 3) and
Reply
#3
Quote:I think you should do it with a getter - setter:

No no no Hand
Python Is Not Java
Quote:Getters and setters are evil. Evil, evil, I say! Python objects are not Java beans. Do not write getters and setters. This is what the 'property' built-in is for. And do not take that to mean that you should write getters and setters, and then wrap them in 'property'. That means that until you prove that you need anything more than a simple attribute access, don't write getters and setters. They are a waste of CPU time, but more important, they are a waste of programmer time. Not just for the people writing the code and tests, but for the people who have to read and understand them as well.
Reply
#4
Ok, you 're telling us what he should 't do, but not what he should.

My second suggestion (haven't tried):


Quote:class MyClasss():
   def __init__(self):      
       self.YesAvailable = ''



  def SearchingForSpace():
     if ((self.YesAvailable == "True") and (len(str(FullNameEntry.get())) > 3) and 
      
      

Reply
#5
You're still using global variables, though. And comparing with a string, instead of just setting it as a bool.

If there's shared information, that information should either...
a) be passed around as an argument/return value, or
b) these should be methods on a class, and YesAvailable should just be an instance variable.

Which option you choose is a choice between project scope and personal preference.
Reply
#6
Quote:My second suggestion (haven't tried):
Yes and can make it look better and something that run.
class MyClass():
    def __init__(self, available):
        self.available = available

    def space_search(self, name_entry):
        if self.available == True and name_entry.get('hello') > 3:
            return 'This is True'
Use:
>>> available = 1
>>> name_entry = {'hello': 999}
>>> obj = MyClass(available)
>>> print(obj.space_search(name_entry))
This is True
@nilamo advice is okay

@Safi133 your code dos not look good at all.
Look how i do naming in the short example over and look/use at PEP-8.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to create a variable only for use inside the scope of a while loop? Radical 10 1,708 Nov-07-2023, 09:49 AM
Last Post: buran
  dynamic variable name declaration in OOP style project problem jacksfrustration 3 786 Oct-22-2023, 10:05 PM
Last Post: deanhystad
  Library scope mike_zah 2 845 Feb-23-2023, 12:20 AM
Last Post: mike_zah
  Scope of variable confusion Mark17 10 2,851 Feb-24-2022, 06:03 PM
Last Post: deanhystad
  Variable scope issue melvin13 2 1,536 Nov-29-2021, 08:26 PM
Last Post: melvin13
  Variable scope - "global x" didn't work... ptrivino 5 3,038 Dec-28-2020, 04:52 PM
Last Post: ptrivino
  Python Closures and Scope muzikman 2 1,812 Dec-14-2020, 11:21 PM
Last Post: muzikman
  Reset Variable problem IcodeUser8 3 2,356 Jul-20-2020, 12:20 PM
Last Post: IcodeUser8
  Variable from notepad problem samuelbachorik 2 2,299 Apr-10-2020, 09:04 AM
Last Post: samuelbachorik
  Block of code, scope of variables and surprising exception arbiel 8 3,422 Apr-06-2020, 07:57 PM
Last Post: arbiel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020