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
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
Related
I have a couple of Classes defined.
Both of these classes take a json file as input, and extract the data into a class instance, and places them into a dictionary.
So all of my EmployeeProfile instances are stored in a dictionary called SP, with the employees email as the key, and the class instance as the value.
All of my RiskProfile instances are stored in a dictionary called RISKS, with the risk_ID as the key and the class instance as the value.
class EmployeeProfile:
def __init__(self, profile):
self.displayname = profile.get('displayName')
self.email = profile.get('email')
self.firstname = profile.get('firstName')
self.surname = profile.get('surname')
self.fullname = profile.get('fullName')
self.costcode = profile.get('work').get('custom')\
.get('Cost Category_t9HHc')
self.title = profile.get('work').get('title')
self.department = profile.get('work').get('department')
self.city = profile.get('work').get('site')
self.id = profile.get('work').get('employeeIdInCompany')
self.manageremail = ''
self.costline = 'N/A'
self.groups = {}
self.attest = {}
class RiskProfile:
def __init__(self, risk_profile):
self.ID = risk_profile.get('ID')
self.key = risk_profile.get('Key')
self.name = risk_profile.get('Name')
self.description = risk_profile.get('Description', '')
self.owner = risk_profile.get("Owner_DisplayName")
self.assignedto = risk_profile.get("AssignedTo_DisplayName")
self.email = None
self.costline = ''
self.notes = ''
self.assessmentlevel = int(risk_profile.get("AssessmentLevel"))
self.rating = ''
self.workflow = risk_profile.get('WorkflowState')
Now when I do something like:
for profile in SP:
print(SP[profile].{here i see the attribute of the class instance})
So I can see a selection of the attributes I may want to print or change etc...
print(SP[profile].department)
or
print(SP[profile].name)
However when I do the same for RiskProfile instances I do not get the list of attributes.
If I enter them manually my code still works, but does anyone know why this is not working the same way?
for profile in RISKS:
print(RISK[profile].{I never get a list of attributes})
I use Anaconda with Spyder.
I found this fantasy name generator here.
I am trying to adapt the code to suit my purpose. I want to create an NPC name automatically, using the function name_gen within the class NPC. With the NPC characteristics being:
class NPC:
def __init__(self, name, age, gender):
self.name = name_gen
self.age = 25
self.gender = M
The code from the name generator I need is the following:
from random import randrange
def line_appender(file_path, target):
file = open(file_path, "r")
splitfile = file.read().splitlines()
for line in splitfile:
target.append(line)
def name_selector(target_list):
selected = target_list[randrange(len(target_list))]
return selected
def name_builder(first_name_list_path, last_name_list_path):
first_name_list = []
last_name_list = []
line_appender(first_name_list_path, first_name_list)
line_appender(last_name_list_path, last_name_list)
first_name_selected = name_selector(first_name_list)
last_name_selected = name_selector(last_name_list)
name = first_name_selected+" "+last_name_selected
return name
Now the only thing I think I still need to do, is to generate the name from within the class NPC. I thought doing something like:
def name_gen
if gender == "M":
name = name_builder("first_name_male.txt", "last_name.txt")
elif gender == "F":
name = name_builder("first_name_female.txt", "last_name.txt")
But I don't understand how to make the name_gen function check the class NPC properties,
so that it generates the desired name.
Could someone perhaps help me out?
EDIT
Thank you for all the solutions! I am pretty new to Python; In order to test Samwises solution, I tried to run it as a separate script (in order to check whether I would get a name) with the code below. It does however not print anything. I'm putting this in an EDIT because I think it might be a trivial question. If it is worth posting a separate question, please let me know:
import random
running = True
npc_input_messsage = "npc = NPC(25, 'M')"
class NameChooser:
def __init__(self, file_path):
with open(file_path) as f:
self._names = f.read().splitlines()
def choice(self):
return random.choice(self._names)
first_choosers = {
"M": NameChooser("first_name_male.txt"),
"F": NameChooser("first_name_female.txt"),
}
last_chooser = NameChooser("last_name.txt")
def name_gen(gender):
return f"{first_choosers[gender].choice()} {last_chooser.choice()}"
class NPC:
def __init__(self, age, gender):
self.name = name_gen(gender)
self.age = age
self.gender = gender
while running:
npc = input(npc_input_messsage)
# I'm entering npc = NPC(25, "M")
print(npc)
Your name generator is a little over-complicated IMO. I'd suggest wrapping all the file reading and name selection stuff in a simple class so you can define it once and then instantiate it for each of your name lists. Putting the file reading part in __init__ means you only do it once per list instead of re-reading the file each time you need to pick a name.
import random
class NameChooser:
def __init__(self, file_path):
with open(file_path) as f:
self._names = f.read().splitlines()
def choice(self):
return random.choice(self._names)
Now you can define three NameChoosers and a name_gen function that picks among them:
first_choosers = {
"M": NameChooser("first_name_male.txt"),
"F": NameChooser("first_name_female.txt"),
}
last_chooser = NameChooser("last_name.txt")
def name_gen(gender):
return f"{first_choosers[gender].choice()} {last_chooser.choice()}"
And now you can define an NPC class that takes age and gender as arguments to the constructor, and picks a random name using name_gen():
class NPC:
def __init__(self, age, gender):
self.name = name_gen(gender)
self.age = age
self.gender = gender
def __str__(self):
return f"{self.name} ({self.age}/{self.gender})"
npc = NPC(25, "M")
print(npc) # prints "Bob Small (25/M)"
I think you're confused about OOP concepts.
First, let's edit your class:
class NPC:
def __init__(self, name, age, gender):
self.name = name
self.age = age
self.gender = gender
See, I have assigned parameter values to the attributes.
Now let's make changes to your function:
def name_gen(gender):
if gender == "M":
name = name_builder("first_name_male.txt", "last_name.txt")
elif gender == "F":
name = name_builder("first_name_female.txt", "last_name.txt")
return name
Here I have added a parameter to your function since you're using its value.
Now let's create an instance for your class.
npc = NPC("Vishwas", 25, "M") # Instance of the class
print(name_gen(npc.gender)) # Print generated name
A straightforward way to make happen automatically would be to simply call the name generator from with the NPC.__init__() method. In the code below it's been made a private method of the class by starting its name with an underscore character. Note that the call to it has to wait until all the instance attributes it references have been assigned value.
from random import randrange
class NPC:
def __init__(self, age, gender):
self.age = age
self.gender = gender
self.name = self._name_gen()
def _name_gen(self):
if self.gender == "M":
name = name_builder("first_name_male.txt", "last_name.txt")
elif self.gender == "F":
name = name_builder("first_name_female.txt", "last_name.txt")
return name
def line_appender(file_path, target):
file = open(file_path, "r")
splitfile = file.read().splitlines()
for line in splitfile:
target.append(line)
def name_selector(target_list):
selected = target_list[randrange(len(target_list))]
return selected
def name_builder(first_name_list_path, last_name_list_path):
first_name_list = []
last_name_list = []
line_appender(first_name_list_path, first_name_list)
line_appender(last_name_list_path, last_name_list)
first_name_selected = name_selector(first_name_list)
last_name_selected = name_selector(last_name_list)
name = first_name_selected+" "+last_name_selected
return name
if __name__ == '__main__':
npc1 = NPC(25, 'M')
print(f'{npc1.name!r}')
npc2 = NPC(21, 'F')
print(f'{npc2.name!r}')
I am trying to get one account from some branch but somewhere i am missing something. This line is from method -> but the result is <main.SavingAccount object at 0x000001F2563CEFD0>
class Branch:
def __init__(self, branch_code, city):
self.branch_code = branch_code
self.city = city
self.account_list = []
self.loan_list = []
def getAccount(self, acc_no):
for account in self.account_list:
if account.acc_no == acc_no:
return account
print(f2.getAccount(300005))
try this:
class Branch:
def __init__(self, branch_code, city):
self.branch_code = branch_code
self.city = city
self.account_list = []
self.loan_list = []
def getAccount(self, acc_no):
for account in self.account_list:
if account == acc_no:
return account
f2 = Branch(123,"NY")
f2.account_list=[111,222,300005]
print(f2.getAccount(300005))
So I was writing a program in Python, which would take all my university classes (from csv) and print info about them. I've wrote a simple class Subject to manage everything better. In my uni there are classes in even weeks, odd weeks, and every-week classes, and I have lectures, exercises and laboratories. So my Subject class is like this:
class Subject:
number = 0
name = ""
dummyData = []
even = {}
odd = {}
all = {}
type = ""
def __init__(self, name, number, type):
self.name = name
self.number = number
self.type = type
self.info = str(number) + " " + name + " " + type
Previously I had all days written in even, odd, and all dicts, like this:
even = {"mon":"",
"tue":"",
"wed":"",
"thu":"",
"fri":"",
}
So I could add all the classes hours to specific day key. But, there was a problem. For example lets say Programming lecture is subject 1 and Programming laboratories are subject 2. Subject 1 is on Monday at 9.15. Subject 2 is on Monday as well, but at 17.05. So I have a function, which would check if the subject is on even/odd week or it is every week. And then I would assign f.e 9.15 to even["mon"] on subject 1. Then I would go for subject 2, and tried to add 17.05 to even["mon"]. Every subject was an other Subject class object stored in a list. But there was a mistake. When I tried to add 17.05 to subject 2s even["mon"] it added it, okay, but then even["mon"] should ="17.05", but it was ="9.15/17.05". I was trying to figure out whats wrong, and I finally did, by changing my class from:
class Subject:
number = 0
name = ""
dummyData = []
even = {"mon":"",
"tue":"",
"wed":"",
"thu":"",
"fri":"",
}
...etc...
type = ""
def __init__(self, name, number, type):
self.name = name
self.number = number
self.type = type
self.info = str(number) + " " + name + " " + type
to:
class Subject:
number = 0
name = ""
dummyData = []
even = {}
odd = {}
all = {}
type = ""
def __init__(self, name, number, type):
self.name = name
self.number = number
self.type = type
self.info = str(number) + " " + name + " " + type
self.even = {"mon":"",
"tue":"",
"wed":"",
"thu":"",
"fri":"",
}
+ odd and all. So why is Python like remembering whats been written into the first object attributes?
You need to declare the attributes inside the __init__ method. Here's an example
class Subject:
def __init__(self, name, number, type):
self.number = number
self.name = name
self.dummyData = []
self.even = {}
self.odd = {}
self.all = {}
self.type = type
Declaring the variables inside the class declaration makes them "class" members and not instance members. Declaring them in the __init__ method makes ensures a new instance of the members is created every time you create a new instance of the object.
I'm trying to figure out how to print out the instance of a class with user defined attributes in Python 3.
Here is the code I have:
class Attendie:
def __init__(self, fname, lname, company, state, email):
self.fname = fname
self.lname = lname
self.company = company
self.state = state
self.email = email
def getFname(self):
return self.fname
def getLname(self):
return self.lname
def getCompany(self):
return self.company
def getState(self):
return self.state
def getEmail(self):
return self.email
def main():
fname = input("what is the attendie's first name? ")
lname = input("What is the attendie's last name? ")
company = input("What company is the attendie with? ")
state = input("What state is the attendie from? ")
email = input("What is the attendie's email address? ")
Person = Attendie(fname, lname, company, state, email)
print(Person.getFname)
print(Person.getLname)
print(Person.getCompany)
print(Person.getState)
print(Person.getEmail)
if __name__ == '__main__': main()
After I run the program I get these types of error messages.
bound method Attendie.getFname of <main.Attendie object at 0x00000000031DCDD8
It seems that you are not calling the methods.
In [1]: class A(object):
...: def __init__(self, n):
...: self.n = n
...: def get_n(self):
...: return self.n
...:
In [2]: a = A(5)
In [3]: a.get_n
Out[3]: <bound method A.get_n of <__main__.A object at 0xa56516c>>
In [4]: a.get_n()
Out[4]: 5
Changing your code as follows should fix it:
print(Person.getFname())
print(Person.getLname())
print(Person.getCompany())
print(Person.getState())
print(Person.getEmail())