colan connon
colan connon

Reputation: 23

Python object scoping issues

I need some help understanding python scoping with classes.

For instance this is a perfectly valid program, which makes sense to me

import json

class Person(object):
    def __init__(self, firstname, lastname, age,address):
        self.firstname = firstname
        self.age = age
        self.lastname = lastname
        self.address = address
class Address(object):
    def __init__(self,zipcode,state):
        self.zipcode = zipcode
        self.state = state



personlist = []
for count in range(5):
    address = Address("41111","statename")
    person = Person("test","test",21,address)
    print(count)
    personlist.append(person)
jsonlist = []
for Person in personlist:
    print Person.firstname
    d = {}
    d['firstname'] = Person.firstname
    d['lastname'] = Person.lastname
    d['age'] = Person.age
    d['zipcode'] = Person.address.zipcode
    d['state'] = Person.address.state
    jsonlist.append(d)
jsondict = {}
jsondict["People"] = jsonlist
jsondict["success"] = 1

json_data = json.dumps(jsondict, indent =4)

print json_data

But this next program gives me an error

import json

class Person(object):
    def __init__(self, firstname, lastname, age,address):
        self.firstname = firstname
        self.age = age
        self.lastname = lastname
        self.address = address
class Address(object):
    def __init__(self,zipcode,state):
        self.zipcode = zipcode
        self.state = state


def main():
    personlist = []
    for count in range(5):
        address = Address("41111","statename")
        person = Person("test","test",21,address)
        print(count)
        personlist.append(person)
    jsonlist = []
    for Person in personlist:
        print Person.firstname
        d = {}
        d['firstname'] = Person.firstname
        d['lastname'] = Person.lastname
        d['age'] = Person.age
        d['zipcode'] = Person.address.zipcode
        d['state'] = Person.address.state
        jsonlist.append(d)
    jsondict = {}
    jsondict["People"] = jsonlist
    jsondict["success"] = 1

    json_data = json.dumps(jsondict, indent =4)

    print json_data
main()

My question is why creating the classes in white space valid but creating them inside a function not valid. Is there any way to create them in the main function and it be valid?

EDIT:

Error is File "jsontest.py", line 9, in main person = Person("test","test",21,address) UnboundLocalError: local variable 'Person' referenced before assignment

Upvotes: 2

Views: 83

Answers (1)

Nir Alfasi
Nir Alfasi

Reputation: 53565

The problem is that you use a variable with the same name as your class Person (also called "shadowing"):

for Person in personlist:

Python detects that you use it as a local variable and raises an error:

UnboundLocalError: local variable 'Person' referenced before assignment

which means that you try to use a local variable before it was assigned in the following line:

person = Person("test","test",21,address)

You can find more information about it here

Upvotes: 5

Related Questions