1

Hi I'm making a program on python and I'm having trouble adding a global variable to my program so I'm just going to post my code and show you how I tried to do it.

So this is my class:

import globalvariables

class Bus :  

def __init__(self, Number, Capacity, Destination, Seats):
    self.Bus_Number = Number
    self.Bus_Capacity = Capacity
    self.Bus_Destination = Destination
    self.Seats_taken = Seats

def Book(self):
    self.Seats_taken = Seats + 1

def ShowBus(self):
    return (str(self.Bus_Number) + ", " + str(self.Bus_Capacity) + ", " + str(self.Bus_Destination) + ", " + str(self.Seats_taken))

and this is my module for global variables

Seats = 0

and this is what I'm trying to run:

import Transport
import globalvariables

Big_Red = Transport.Bus(1, 50, "NYC", 0)
Big_Red.Book()

print(Big_Red.ShowBus())

I'm getting this error:

Traceback (most recent call last):
  File "D:\Python\Assignment 3\Tester.py", line 5, in <module>
    Big_Red.Book()
  File "D:\Python\Assignment 3\Transport.py", line 14, in Book
    self.Seats_taken = Seats + 1
NameError: global name 'Seats' is not defined

3 Answers 3

1

The variable Seats is local to __init__ function and can't be accessed outside of it.

So,

self.Seats_taken = Seats + 1

Should be :

self.Seats_taken =  self.Seats_taken + 1

or :

self.Seats_taken += 1 

Instead of using global variables inside class you should use class attributes:

class Bus :
    seats = 50  #shared across all instances
    def __init__(self):
        #code 
    def Book(self):
        self.Seats_taken = self.seats + 1
Sign up to request clarification or add additional context in comments.

3 Comments

thanks brah but if i did want to make a module of global variables and use that in my bus class, how would i do it?
@Panthy In which file did you define Seats variable?
@Panthy If it's inside globalvariables then use globalvariables.Seats. You should better create a class attribute Seats.
1

Globals should be avoided. In case you still want it to be :

def Book(self):
    self.Seats_taken = globalvariables.Seats + 1

Comments

0

When you import globalvariables, you gain access to names qualified by the module name: globalvariables.Seats. To import Seats into the namespace of another module, use from globalvariables import Seats. (In desperate cases, you can import all names from a module: from globalvariables import *, but usually you don't want this.)

When you define a function, it has its own local namespace. It includes all function's arguments.

Seats = 100

def foo(Seats):
  # returns the value of variable named Seats
  # defined within "def foo", *not* defined by "Seats = 100"
  return Seats

print foo(200) # prints 200
print foo() # fails because Seats are not set

To initialize a function parameter, use default value:

def foo(seats=0):

print foo() # prints 0
print foo(55) # prints 55  

Also, global variables are evil. Global constants are good.

You want to use a global variable to track seats taken. You'll be much better off if you encapsulate it into a class that only allows reasonable access, does not allow to set the value arbitrarily, log access if needed, etc:

class SeatsDispenser(object):
  def __init__(self, initial_count):
    self.count = initial_count

  def allocate(self, number_of_seats):
    self.count -= number_of_seats
    if self.count < 0:
      raise ValueError("Overcommitted!")

  def seats_left(self):
    return self.number_of_seats

Naming your variables, classes, constants, and functions with the same Title Case is impractical. Usually variables are lower_case, functions are lowerCamelCase, classes are TitleCamelCase and constants are ALL_CAPS.

A reasonable piece of code would look like this:

import constants # modules are usually lower case
import transport

def Bus(object):
  def __init__(self, number, capacity, seats=constants.SEATS):
    self.number = number
    self.capacity = capacity
    self.seats = seats

big_red = Bus(constants.NYC_BUS_NUMBER, 50, 25)
default_blue = Bus(1, 20) # seats not passed, the default value is used

seats_dispenser = SeatsDispenser(100)
seats_dispenser.allocate(big_red.count)
seats_dispenser.allocate(default_blue.count)
print seats_dispenser.seats.left()

Comments

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.