4

我有以下代码:

#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys import re

companies = {}
for line in open('/home/ibrahim/Desktop/Test.list'):
    company, founding_year, number_of_employee = line.split(',')
    number, name = company.split(")")
    companies[name] = [name, founding_year, number_of_employee]
    print "Company: %s" % company

CompanyIndex = raw_input('\n<Choose a company you want to know more about.>\n\n<Insert a companyspecific-number and press "Enter" .>\n')

if CompanyIndex in companies:
    name, founding_year, number_of_employee = companies[CompanyIndex]
    print 'The companys name is: ',name,'\nThe founding year is: ', founding_year,'\nThe amount of employees is: ', number_of_employee
else:
    print"Your input is wrong."

该程序从如下所示的文本文件中读取一些信息:

(1)Chef,1956,10
(2)Fisher,1995,20
(3)Gardener,1998,50

我的目标是创建一个类,我可以在其中保存有关公司名称、成立年份和员工人数的信息,而不是使用包含列表的字典。

我阅读了几个教程,但我真的不知道该怎么做。__init__这个“自我”是什么,做了什么__del__等等,真是令人困惑。我该怎么做呢?

4

4 回答 4

2

你可以做:

class Company(object):

    def __init__(self, name, founding_year, number_of_employee):
        self.name = name
        self.founding_year = founding_year
        self.number_of_employee = number_of_employee

Company之后,您可以通过编写来创建对象company = Company('Chef', 1956, 10)

于 2013-09-11T14:21:38.857 回答
1
class companies(object):
    def __init__(self,text_name):
        text_file = open(text_name,'r')
        companies = {}
        all_text = text_file.read()
        line = all_text.split('\n')  #line is a list
        for element in line:
            name,year,number = element.split(',')
            companies[name] = [year,number]
        self.companies = companies

    def get_information(self,index):
        print self.companies[index]

 #an instance of the class defined above
my_company = companies(r'company.txt')
 #call function of my_company
my_company.get_information(r'Gardener')
于 2013-09-11T15:50:48.570 回答
1

这是一个如何创建CompanyInfo类的示例。

class CompanyInfo(object):
    def __init__(self, name, founded_yr, empl_count):
        self.name = name
        self.founded_yr = founded_yr
        self.empl_count = empl_count

    def __str__(self):
        return 'Name: {}, Founded: {}, Employee Count: {}'.format(self.name, self.founded_yr, self.empl_count)

这是一个如何创建它的示例:

# ...
for line in open('/home/ibrahim/Desktop/Test.list'):
    company, founding_year, number_of_employee = line.split(',')
    comp_info = CompanyInfo(company, founding_year, number_of_employee)

这是一个如何使用它的示例:

print "The company's info is:", str(comp_info)
于 2013-09-11T14:29:54.337 回答
-1
class Company:
    def __init__(self, name, year_of_funding, num_of_employees):
        '''
        This is the constructor for the class. We pass the
        info as arguments, and save them as class member variables
        '''
        self.name = name
        self.year_of_funding = year_of_funding
        self.num_of_employees = num_of_employees

    def get_name(self):
        '''
        This method returns the company name
        '''
        return self.name

    def get_year_of_funding(self):
        '''
        This method returns the year the company was funded
        '''
        return self.year_of_funding

    def get_num_of_employees(self):
        '''
        This method returns the number of employees this company has
        '''
        return self.num_of_employees

然后您可以创建该类的实例,并使用 get 方法获取数据:

my_company = Company('Microsoft', 1964, 30000)
print my_company.get_name() + ' was funded in ' + str(my_company.get_year_of_funding()) + ' and has ' + str(my_company.get_num_of_employees()) + ' employees'
# OUTPUT: Microsoft was funded in 1964 and has 30000 employees
于 2013-09-11T14:26:26.103 回答