温馨提示:本文翻译自stackoverflow.com,查看原文请点击:python 3.x - how to use datetime objects in a class instance?

python 3.x - 如何在类实例中使用datetime对象?

发布于 2020-03-31 23:20:47

在for循环中创建Rabbit类的实例,并给它一个datetime对象作为init变量,但是所有实例都具有相同的datetime值。我预计每个实例都会有时差。我在这里缺少了解什么?

from datetime import datetime
class Rabbit_birth:
    num_eyes = 2  # <- Class variable
    num_legs = 4
    num_ears = 2
    num_tails = 1
    num_of_rabbits = 0

    def __init__(self, name = 'Rabbit',
                 fur_colour = 'unknown',
                 eye_colour = 'unknown',
                 sex = 'unsexed',
                 breed = 'unknown',
                 dob = datetime.now(),
                 tob = datetime.now()
                 ):

        self.name = name  # <- Instance variable
        self.fur_colour = fur_colour
        self.eye_colour = eye_colour
        self.sex = sex
        self.breed = breed
        self.dob = str(dob.strftime("%x"))
        self.tob = str(tob.strftime("%X"))
        Rabbit_birth.num_of_rabbits += 1


    def display_rabbit(self):
        dob_time = self.dob+" at "+self.tob
        return ("{0.name} is a {0.sex} rabbit"
    " and has {0.fur_colour} fur and "
    "{0.eye_colour} eyes, {0.name} is "
    "of the {0.breed} breed of rabbits.\n"
    "{0.name}'s DOB is ").format(self) + dob_time         
################################################
print()

print('Number of rabbits is ',Rabbit_birth.num_of_rabbits)





rabbits=[]
def create_rabbit():
    lr=[]
    rabbit = ('name', 'fur colour', 'eye colour', 'sex', 'breed')
    for item in rabbit:
        r = input(item+' >')
        lr.append(r)
    r0,r1,r2,r3,r4=lr
    name = Rabbit_birth(r0,r1,r2,r3,r4)    
    return name
for i in range(3):

    name=create_rabbit()
    rabbits.append(name)
    print(rabbits[i].display_rabbit())

查看更多

提问者
Susan
被浏览
17
Susan 2020-01-31 19:24

我看到了我犯的错误,这是在错误的范围内传递datetime对象的问题。

这是相关的更正代码:

## <- class experiment
from datetime import datetime
class Rabbit_birth:
    num_eyes = 2  # <- Class variable
    num_legs = 4
    num_ears = 2
    num_tails = 1
    num_of_rabbits = 0

    def __init__(self, name = 'Rabbit',
                 fur_colour = 'unknown',
                 eye_colour = 'unknown',
                 sex = 'unsexed',
                 breed = 'unknown',
                 dob = None, # This line changed
                 tob = None  # This line changed
                 ):
        self.name = name  # <- Instance variable
        self.fur_colour = fur_colour
        self.eye_colour = eye_colour
        self.sex = sex
        self.breed = breed
        self.dob = datetime.now() # This line changed
        self.tob = datetime.now() # This line changed
        Rabbit_birth.num_of_rabbits += 1


    def display_rabbit(self):
        # This line below changed
        dob_time = str(self.dob.strftime("%x"))+" at "+str(self.tob.strftime("%X"))
        return ("{0.name} is a {0.sex} rabbit"
    " and has {0.fur_colour} fur and "
    "{0.eye_colour} eyes, {0.name} is "
    "of the {0.breed} breed of rabbits.\n"
    "{0.name}'s DOB is ").format(self) + dob_time         
################################################

发布
问题

分享
好友

手机
浏览

扫码手机浏览