这是一个家庭作业问题,但正如您所看到的,我已经编写了大部分代码。 :)我想要发生的是代理对象继承投资者对象的方法,因为代理对象的方法是假设基本相同。唯一不同的是构造函数,投资者需要6个参数,代理需要7个。您可以看到参数也基本相同。
Investor.load 和 Investor.report 大部分都按预期工作。 代理中的继承方法不会。见下文。
我的主要问题:
谢谢!
会在转折日期后重新添加。
以上是上面的代码应该的功能。
>>> from sys import stdout as o
>>> import datetime
>>> from hedgefunds import *
>>>
>>> i = Investor('Flintstone','Fred',datetime.date(1965,12,3),'male','Mr.')
>>>
>>> i
Investor('Flintstone', 'Fred', datetime.date(1965, 12, 3), 'male', 'Mr.', 1)
>>> repr(i)
"Investor('Flintstone', 'Fred', datetime.date(1965, 12, 3), 'male', 'Mr.', 1)"
>>> i.__repr__() # THIS CONTROLS PREVIOUS TWO LINES
"Investor('Flintstone', 'Fred', datetime.date(1965, 12, 3), 'male', 'Mr.', 1)"
>>> eval(repr(i)) # OBJECT CAN BE RECREATED FROM ITS REPR STRING
Investor('Flintstone', 'Fred', datetime.date(1965, 12, 3), 'male', 'Mr.', 1)
>>>
>>> i # NOTICE ONE ADDITIONAL BIT OF DATA WE DIDN'T SPECIFY?
Investor('Flintstone', 'Fred', datetime.date(1965, 12, 3), 'male', 'Mr.', 1)
>>> # THE '1' AT THE END IS THE RECORD ID rid. THE REPR STRING INCLUDES ALL DATA SO YOU SEE IT.
>>> # THE INVESTOR CLASS SHOULD ASSIGN A rid IF ONE IS NOT PASSED INTO THE CONSTRUTOR.
>>>
>>> print i
1, Mr. Fred Flintstone, 12/03/1965, male
>>> i.__str__() # THIS CONTROLS PREVIOUS LINE
'1, Mr. Fred Flintstone, 12/03/1965, male'
>>>
>>> # NOTE THE NAME IS NICELY PUT TOGETHER AND AND rid IS AT THE START
>>> # NOTE datetime.date(YYYY, MM, DD) BUT WE PRINT IN MM/DD/YYYY FORMAT
>>>
>>> # NOW LET'S LOOK AT OUR INVESTOR OBJECT'S ATTRIBUTES
>>> i.last_name
'Flintstone'
>>> i.first_name
'Fred'
>>> i.dob
datetime.date(1965, 12, 3)
>>> i.date_of_birth
'12/03/1965'
>>> i.gender
'male'
>>> i.title
'Mr.'
>>> i.name
'Mr. Fred Flintstone'
>>> i.rid
1
>>>
>>> # BUT WE'RE GONNA NEED SOMEHWERE TO STORE MULTIPLE INVESTOR OBJECTS
>>> # SO AS WELL AS CREATING INVESTOR OBJECTS, THE INVESTOR CLASS MUST ACT LIKE A RECORDSET
>>>
>>> Investor.report(o) # o WAS SET TO STDOUT AT THE TOP OF THIS TRACE BUT COULD BE ANY FILE
>>> Investor.add(i) # NO RECORDS SO LET'S ADD ONE
>>> Investor.report(o) # NOW WE HAVE SOMETHING TO REPORT
1, Mr. Fred Flintstone, 12/03/1965, male
>>>
>>> Investor.add(Investor('Flintstone','Wilma',datetime.date(1968,1,15),'female','Mrs.'))
>>> Investor.report(o)
1, Mr. Fred Flintstone, 12/03/1965, male
2, Mrs. Wilma Flintstone, 01/15/1968, female
>>>
>>> # WE COULD CONTINUE ADDING INVESTORS MANUALLY BUT SINCE WE'VE GOT A FILE FULL OF THEM....
>>> Investor.load('investors.csv')
>>> Investor.report(o)
1, Mr. Charles Creed, 12/05/1928, male
2, Miss Sheila Geller, 11/12/1962, female
3, Mr. Fred Kenobi, 07/13/1957, male
4, Miss Rachel Geller, 07/11/1968, female
5, Mr. Charles Rubble, 09/23/1940, male
6, Mrs. Leah Skywalker, 07/02/1929, female
7, Mr. Bill Balboa, 03/06/1988, male
8, Dr. Sheila Barkley, 08/26/1950, female
.
.
>>> # YOU SHOULD SEE 120 RECORDS (OUR TWO MANUALLY ADDED RECORDS ARE GONE)
>>>
>>>
>>>
>>> # AGENTS
>>>
>>> a = Agent(2.1,'Rubble','Barney',datetime.date(1966,4,20),'male','Mr.')
>>> a
Agent(2.1000000000000001, 'Rubble', 'Barney', datetime.date(1966, 4, 20), 'male', 'Mr.', 1)
>>> repr(a)
"Agent(2.1000000000000001, 'Rubble', 'Barney', datetime.date(1966, 4, 20), 'male', 'Mr.', 1)"
>>> eval(repr(a))
Agent(2.1000000000000001, 'Rubble', 'Barney', datetime.date(1966, 4, 20), 'male', 'Mr.', 1)
>>> print a
1, Mr. Barney Rubble, 04/20/1966, male, 2.1
>>> a.last_name
'Rubble'
>>> a.first_name
'Barney'
>>> a.dob
datetime.date(1966, 4, 20)
>>> a.date_of_birth
'04/20/1966'
>>> a.gender
'male'
>>> a.title
'Mr.'
>>> a.name
'Mr. Barney Rubble'
>>> a.rid
1
>>> a.commission
2.1000000000000001
>>> Agent.add(a)
>>> Agent.report(o)
1, Mr. Barney Rubble, 04/20/1966, male, 2.1
>>> Agent.load('agents.csv')
>>> Agent.report(o)
1, Mr. Barney Flintstone, 02/13/1933, male, 4.0
2, Miss Rachel Rubble, 11/21/1982, female, 2.5
3, Dr. Ted Geller, 03/14/1963, male, 8.0
4, Miss Phoebe Creed, 11/06/1959, female, 5.5
5, Mr. Luke Kenobi, 08/24/1945, male, 2.5
6, Dr. Megan Creed, 03/26/1957, female, 5.5
7, Mr. Ted Rubble, 09/14/1931, male, 3.5
8, Mrs. Monica Balboa, 05/07/1934, female, 1.5
答案 0 :(得分:2)
这里有很多事情发生,所以我会尽量简短。
class Investor(object):
reports = []
data = []
def __init__(self, LastName, FirstName, Date, Gender, Title, Rid=1):
# the following two lines define variables which only exist inside
# __init__, so they can basically be gotten rid of.
# reports = []
# data = []
# but they *don't need to be initialized*. Actually creating a new list
# in each constructor is *bad* unless you want one report per instance
# ... later, be sure to add "self" instead of adding to the local vars
self.add(self)
@classmethod
def report(self, o):
result = ""
for x in self.reports:
# Just use join, that's what you mean anyway.
result = ', '.join(x)
# \n should result in \n\n. I think you mean o.write(result)
o.write(result + "\n")
@classmethod
def load(self, f):
self.reports = []
self.data = []
file = open(f)
# why the extra readline?
file.readline()
list = []
while 1:
line = file.readline()
# you need to get rid of the trailing whitespace
items = line[:-1].split(", ")
if not line:
break
else:
# self, here, refers to the class, so you can use it as the
# constructor as well
# use the * to make a list into an argument list.
self(*items)
# No need to add it, the value is added at the end of __init__
# as things stand.
class Agent(Investor):
reports = []
data = []
def __init__(self, Commission, LastName, FirstName, \
Date, Gender, Title, Rid=1):
super(Agent, self).__init__(self, LastName, FirstName, Date, \
Gender, Title, Rid)
self.commission = Commission
def __str__(self):
# Be lazy! Use super!
return super(Agent,self).__str__() + ', ' + self.commission
# this was doing the same thing in the parent class.
# def __repr__(self):
# The classmethod methods are not needed unless you want to redefine
# behavior in a child class.
@classmethod
def add(self, i):
# If you do change something, though, make sure to use super
# here as well...
super(Agent, self).add(i)
我认为应该照顾你的大部分问题。
如果您想使用格式Investor('Flintstone', 'Fred', datetime.date(1965, 12, 3), 'male', 'Mr.', 1)
,则需要包含__name__
并引用属性:
def __repr__(self):
# notice the " and ' below.
return self.__name__ + "('" + str(self.rid) + "', '" + \
self.name + "', " + self.date_of_birth + ", '" + self.gender + "')"
在Agent中,您需要使用子字符串来获取所需内容:
def __repr__(self):
# remove the Agent( so you can add the commission at the *front*
s = super(Agent,self).__str__()
return self.__name__ + "('" + self.commission + "'," + \
s[len(self.__name__ + 1):]