简体   繁体   English

带有内置输入函数的类方法(Python)

[英]Class method with built-in input function (Python)

I have to following exercise:我必须进行以下练习:

Implement a class PersonReader supporting the following methods:实现一个支持以下方法的类 PersonReader:

input() asks the user for the name and year of birth of a person at shell prompt (using the builtin input function). input() 在 shell 提示下询问用户姓名和出生年份(使用内置输入函数)。

str that returns the string "name (year)" (eg to be used by the print method when applied to a PersonReader). str返回字符串“name (year)”(例如,在应用于 PersonReader 时由 print 方法使用)。

And my idea was to do something like this:我的想法是做这样的事情:

class Personreader:

    def __init__(self, name, year): 
        self.name = name
        self.year = year

    def from_input(x): 
        return x(input(),input())

    def __str__(self):
        print(self.name, self.year)

However this gives an error when I try and call Personreader.from_input(x) or Personreader.from_input().但是,当我尝试调用 Personreader.from_input(x) 或 Personreader.from_input() 时,会出现错误。 How can implement this user input in my class?如何在我的班级中实现此用户输入?

You've defined from_input as a regular method , this means its first parameter is always self (no matter what name you give it) and it has to be either called on an instance or provided with an instance.您已将from_input定义为常规方法,这意味着它的第一个参数始终是self (无论您给它起什么名字),并且必须在实例上调用它或随实例一起提供。

To make from_input an alternative constructor (which seems to be the intent here) you should make it into a class method by decorating it with @classmethod .要使from_input成为替代构造函数(这似乎是此处的意图),您应该通过用@classmethod装饰它来使其成为类方法 In that case, the name of the (first) parameter should be cls eg在这种情况下,(第一个)参数的名称应该是cls例如

    @classmethod
    def from_input(cls):
        return cls(input(), input())

Incidentally, your implementation of __str__ is misguided.顺便说一句,您对__str__的实现是错误的。 __str__ should return a visualisation of the object as a string, it should not print it. __str__应该以字符串形式返回对象的可视化,而不应该打印它。 If you want a printer of some sort, then add a method with that naming eg print_object .如果您想要某种类型的打印机,请添加具有该命名的方法,例如print_object Alternatively, fix your __str__ then just print(reader) or whatever.或者,修复你的__str__然后只是print(reader)或其他什么。

Finally, classes are usually CamelCased in python, so it shoud be PersonReader not Personreader .最后,类在 python 中通常是 CamelCased,所以它应该是PersonReader而不是Personreader Though it's unclear what the reader part is for, your class is just a person, which can incidentally be defined from inputs (colloquially readers are objects which can load data from file or file-like objects eg csv.reader , so a PersonReader would be something which parses a file or file-like object and loads one or more Person objects).虽然不清楚reader部分的用途,但您的类只是一个人,可以顺便从输入中定义(通俗地说, reader是可以从文件或类似文件的对象(例如csv.reader加载数据的对象,因此PersonReader将是解析文件或类似文件的对象并加载一个或多个Person对象的东西)。

I agree with the points mentioned in answer given by @Masklinn我同意@Masklinn 给出的回答中提到的观点

Here's what you can to make it working这是您可以使用的方法

class PersonReader:

 def __init__(self, name, year):
    self.name = name
    self.year = year
  @staticmethod
  def from_input():
    return PersonReader(input(),input())
  @classmethod
 def __str__(self):
    return str(self.name)+" ("+str(self.year)+")" #output as per required

p1 = PersonReader.from_input() #create person from input
print(p1.name) # name 
print(p1.year) # year
print(p1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM