目录
TypeError: missing 1 required positional argument: ‘self’
TypeError: missing 1 required positional argument: ‘self’
Python“TypeError: missing 1 required positional argument: ‘self’”发生在我们在类而不是类的实例上调用方法时。
要解决该错误,请先实例化类并调用实例上的方法,例如emp1.get_name()
。
下面是错误如何发生的示例。
class Employee(): def __init__(self, name, salary): self.name = name self.salary = salary def get_name(self): return self.name # ⛔️ TypeError: Employee.get_name() missing 1 required positional argument: 'self' print(Employee.get_name())
get_name
我们在类上而不是在导致错误的Employee
实例上调用该方法。Employee
实例化类并在实例上调用方法
要解决错误,请实例化类并调用实例上的方法。
class Employee(): def __init__(self, name, salary): self.name = name self.salary = salary def get_name(self): return self.name # ✅ instantiate class first emp1 = Employee('Bobby Hadz', 100) # ✅ call method on class instance print(emp1.get_name()) # 👉️ "Bobby Hadz"
我们实例化了类并get_name
在实例上调用了方法。
__init__()
除外)。 self
当一个类定义了
_ _ init _ _ ()方法时,该方法在创建实例时被调用。
如果您的类没有定义__init__()
方法,则在实例化它时不必传递任何参数。
__init__()
。请注意,该方法采用的第一个参数__init__()
是self
。
您可以将此参数命名为任何名称,因为该名称self
在 Python 中没有特殊含义。
self
表示该类的一个实例,因此当我们将一个变量赋值为 时
self.my_var = 'some value'
,我们声明了一个实例变量——每个实例的唯一变量。
“TypeError: missing 1 required positional argument: ‘self’” 的发生是因为我们self
在get_name
方法中指定了参数。
class Employee(): def __init__(self, name, salary): self.name = name self.salary = salary # 👇️ specified self arg def get_name(self): return self.name emp1 = Employee('Bobby Hadz', 100) print(emp1.get_name()) # 👉️ "Bobby Hadz"
get_name
如果我们在类的实例上调用方法,Python 会自动传递self
给类方法。
使用静态类方法而不是实例方法
如果您的方法不使用参数self
,您可以声明一个
静态方法。
class Employee(): def __init__(self, name, salary): self.name = name self.salary = salary @staticmethod def get_name(): return 'Bobby Hadz' print(Employee.get_name()) # 👉️ "Bobby Hadz"
静态方法不接收隐式第一个参数,可以在类或类的实例上调用。
您还可以使用@classmethod装饰器将方法转换为类方法
。
class Employee(): def __init__(self, name, salary): self.name = name self.salary = salary @classmethod def get_name(cls): print(cls) return 'Alice' print(Employee.get_name()) # 👉️ "Bobby Hadz"
类方法作为隐式第一个参数传递给类,就像实例方法传递给实例一样。
您可以在类 ( Employee.get_name()
) 或类的实例 ( Employee('Alice', 100).get_name()
) 上调用类方法。
如果您在类的实例上调用类方法,则该实例将被忽略,但它的类除外。
# load() missing 1 required positional argument: ‘Loader’
The Python “TypeError: load() missing 1 required positional argument:
‘Loader'” occurs when we use the yaml.load()
method without specifying the
Loader
keyword argument.
To solve the error, use the yaml.full_load()
method instead or explicitly
set the Loader
keyword arg.
Here is an example of how the error occurs.
import yaml document = """ a: 1 b: c: 3 d: 4 """ # ⛔️ TypeError: load() missing 1 required positional argument: 'Loader' print(yaml.dump(yaml.load(document)))
The yaml.load
method now
requires
us to explicitly specify the Loader
keyword arguments because of some security
implications around the default behavior of the method.
The easiest way to solve the error is to use one of the yaml.full_load()
or
yaml.safe_load()
methods.
import yaml document = """ a: 1 b: c: 3 d: 4 """ print(yaml.dump(yaml.safe_load(document))) print(yaml.dump(yaml.full_load(document)))
The examples above achieve the same result as explicitly passing the Loader
keyword argument in a call to the yaml.load()
method.
import yaml document = """ a: 1 b: c: 3 d: 4 """ print(yaml.dump(yaml.load(document, Loader=yaml.SafeLoader))) print(yaml.dump(yaml.load(document, Loader=yaml.FullLoader)))
The SafeLoader
which is used by the yaml.safe_load()
method loads a subset
of the YAML language. This is recommended for loading untrusted input.
yaml.safe_load
method should be used unless you need to serialize or deserialize arbitrary objects because the method cannot execute arbitrary code from the YAML file.The FullLoader
which is used by the yaml.full_load
method loads the full
YAML language. Using this with untrusted input is not recommended.
You can read more about why the default behavior of the yaml.load
method was
deprecated in
this Github wiki.
# Additional Resources
您可以通过查看以下教程来了解有关相关主题的更多信息: