如何避免这段代码中的 pylint(not-an-iterable)?
how to avaid pylint(not-an-iterable) in this code?
相似:问题:
刚开始写代码,看不懂上面的代码
此代码有效,但我在第二次定义中收到两个警告错误
1,pylint(no-self-argument)(但我在 class 上使用它)
2,pylint(not-an-iterable)(在 2nd def 的客户列表中)
class Customer:
def __init__(self, name, membership_type):
self.name = name
self.membership_type = membership_type
def print_all_customers(customerlist):
for customer in customerlist:
print(customer,'\n')
customerlist = [Customer('shagun','gold'),Customer('hero','diamond'),Customer('sid','gold')]
Customer.print_all_customers(customerlist)
如何避免这段代码中出现这些错误,请简单说明
您的代码有两个主要问题。首先,您需要一个字符串方法来提供 Customer 对象的字符串表示形式,以便您可以打印它。其次,您的 print_all_customers() 函数应该在您的 class 之外——它不是合适的 class 方法。修复您的代码也可以消除 pylint 错误。
class Customer:
def __init__(self, name, membership_type):
self.name = name
self.membership_type = membership_type
def __str__(self) -> str:
return f'Customer {self.name} is a {self.membership_type} member.'
def print_all_customers(lst):
for customer in lst:
print(customer)
customerlist = [Customer('shagun','gold'),Customer('hero','diamond'),Customer('sid','gold')]
print_all_customers(customerlist)
#prints:
#Customer shagun is a gold member.
#Customer hero is a diamond member.
#Customer sid is a gold member.
我对另一个答案投了赞成票,pakpe 给出的是代码最后应该如何看待。确实存在设计问题
考虑到这一点,您的基本问题是实例方法应该将 'self' 作为第一个参数。如果你想要一个静态函数,你应该用 staticmethod:
装饰它
即:
class Customer:
def print_all_customers(self, customers):
...
customer = Customer()
customer.print_all_customers(customers)
或:
class Customer:
@staticmethod
def print_all_customers(customers):
...
Customer.print_all_customers(customers)
如果没有 @staticmethod
,我们希望第一个参数是 self
,即 Customer 实例,而不是列表。另见 Difference between staticmethod and classmethod
相似:问题:
刚开始写代码,看不懂上面的代码
此代码有效,但我在第二次定义中收到两个警告错误
1,pylint(no-self-argument)(但我在 class 上使用它)
2,pylint(not-an-iterable)(在 2nd def 的客户列表中)
class Customer:
def __init__(self, name, membership_type):
self.name = name
self.membership_type = membership_type
def print_all_customers(customerlist):
for customer in customerlist:
print(customer,'\n')
customerlist = [Customer('shagun','gold'),Customer('hero','diamond'),Customer('sid','gold')]
Customer.print_all_customers(customerlist)
如何避免这段代码中出现这些错误,请简单说明
您的代码有两个主要问题。首先,您需要一个字符串方法来提供 Customer 对象的字符串表示形式,以便您可以打印它。其次,您的 print_all_customers() 函数应该在您的 class 之外——它不是合适的 class 方法。修复您的代码也可以消除 pylint 错误。
class Customer:
def __init__(self, name, membership_type):
self.name = name
self.membership_type = membership_type
def __str__(self) -> str:
return f'Customer {self.name} is a {self.membership_type} member.'
def print_all_customers(lst):
for customer in lst:
print(customer)
customerlist = [Customer('shagun','gold'),Customer('hero','diamond'),Customer('sid','gold')]
print_all_customers(customerlist)
#prints:
#Customer shagun is a gold member.
#Customer hero is a diamond member.
#Customer sid is a gold member.
我对另一个答案投了赞成票,pakpe 给出的是代码最后应该如何看待。确实存在设计问题
考虑到这一点,您的基本问题是实例方法应该将 'self' 作为第一个参数。如果你想要一个静态函数,你应该用 staticmethod:
装饰它即:
class Customer:
def print_all_customers(self, customers):
...
customer = Customer()
customer.print_all_customers(customers)
或:
class Customer:
@staticmethod
def print_all_customers(customers):
...
Customer.print_all_customers(customers)
如果没有 @staticmethod
,我们希望第一个参数是 self
,即 Customer 实例,而不是列表。另见 Difference between staticmethod and classmethod