我正在编写自己的课程和方法,除了使用localtime()
确定用户年龄的部分外,我完成了所有这些工作。我从未使用localtime()
。所以我不知道如何在代码中实现它。目前,按照它编写的方式,它会返回一个绑定错误。
# -*- coding: utf-8 -*-
"""
Created on Sun Nov 15 22:10:07 2015
@author: Daddy
"""
class Person ():
def __init__(self, name, birthyear):
self.name = name
self.birthyear = birthyear
def age(self, birthyear):
age = 2015 - self.birthyear
return age
def name(self):
return self.name
class Instructor(Person):
def __init__(self, name, birthyear, degree):
self.name = name
self.birthyear = birthyear
self.degree = degree
def degree(self, degree):
return (self.degree)
class Student(Person):
def __init__(self, name, birthyear, major):
self.name = name
self.birthyear = birthyear
self.major = major
def major(self, major):
return (self.major)
答案 0 :(得分:1)
import datetime
def age(self):
return datetime.datetime.now().year - self.birthyear
请注意,您不需要通过birthyear因为它是self,而return是被调用函数的值。