在学习python的过程中,此代码有错误:
class User:
def __init__(self, first_name, last_name, location, payment_method):
self.first_name = first_name
self.last_name = last_name
self.location = location
self.payment_method = payment_method
self.login_attempts = 0
def increment_login_attempts(self):
self.login_attempts += 1
def reset_login_attempts(self):
self.login_attempts = 0
def printing_login_attempts(self):
print('Quantity of login attempts: ' + str(self.login_attempts))
def describe_user(self):
print('Current user first name: ' + self.first_name.title())
print('Current user last name: ' + self.last_name.title())
print('Location is ' + self.location.title() + '. Payment method is ' + self.payment_method)
def greet_user(self):
print('Hello ' + self.first_name.title() + ' ' + self.last_name.title() + '!!!')
class Admin(User):
def __init__(self, first_name, last_name, location, payment_method):
super().__init__(first_name, last_name, location, payment_method)
self.privilegis = Privilegis()
class Privilegis:
def __init__(self, privilegis_type):
self.privilegis_type = ['Allow to delete users', 'Allow to rename users', 'Allow to ban users']
def show_privilegis(self):
print('Special admins privilegis are: ' + ', '.join(self.privilegis))
new_user = Admin('John', 'Edwards', 'LA', 'visa')
new_user.privilegis.show_privilegis()
错误描述
self.privilegis = Privilegis()
TypeError: __init__() missing 1 required positional argument: 'privilegis_type'
我无法理解问题所在。我应该如何使用一个类(Privilegis)作为另一个类(Admin)的属性?
答案 0 :(得分:1)
在您的#!/usr/bin/env pwsh
# Verify user's Git config has appropriate email address
if ($env:GIT_AUTHOR_EMAIL -notmatch '@(non\.)?acme\.com$') {
Write-Warning "Your Git email address '$env:GIT_AUTHOR_EMAIL' is not configured correctly."
Write-Warning "It should end with '@acme.com' or '@non.acme.com'."
Write-Warning "Use the command: 'git config --global user.email <name@acme.com>' to set it correctly."
exit 1
}
exit 0
类中,您编写:
Privileges
因此,您指定它需要一个class Privileges:
def __init__(self, privilegis_type):
self.privilegis_type = ['Allow to delete users', 'Allow to rename users', 'Allow to ban users']
# ...
参数,但这很奇怪,因为稍后您将不对其进行任何操作。
您可能应该重写构造函数并处理参数(从而在构造privilegis_type
对象时提供一个参数,否则就应该删除该参数。
答案 1 :(得分:1)
您的Admin
类具有privilegis
属性,该属性初始化Privilegis
的实例。 __init__()
方法定义了如何初始化类,在您的情况下,它需要使用privilegis_type
作为参数。
如果您希望在实例化时将此特权传递给Admin
,请将参数添加到其__init__
方法中,并将其内部传递给privilege
属性。
class Admin(User):
def __init__(self, first_name, last_name, location, payment_method, privilegis_type): # add privilegis type
super().__init__(first_name, last_name, location, payment_method)
self.privilegis = Privilegis(privilegis_type) # pass that privilegis type, needed by the Privilegis class
class Privilegis:
def __init__(self, privilegis_type):
self.privilegis_type = ['Allow to delete users', 'Allow to rename users', 'Allow to ban users']
def show_privilegis(self):
print('Special admins privilegis are: ' + ', '.join(self.privilegis_type)) # fixed that also
尽管在您的代码中有两个主要错误:
Privilegis
类根本没有使用参数。您可以保留原始代码,并使用合理的默认值删除 privilegis_type
或 ,具体取决于您要尝试的操作要做。self.privilegis
在Privilegis
内部未定义。我相信您希望它成为self.privilegis_type
这意味着有这样的东西:
class Privilegis:
def __init__(self, privilegis_type=None): # could be anything
self.privilegis_type = ['Allow to delete users', 'Allow to rename users', 'Allow to ban users']
def show_privilegis(self):
print('Special admins privilegis are: ' + ', '.join(self.privilegis_type)) # fixed that also