账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    Python: missing 1 required positional argument: 'self'
    416
    0

    有这么一段代码,我想做到的是:
    1、__name不可直接访问,类似与private或protect
    2、用__get_name获取__name值,把执行结果赋予Name

    #!/bin/env python3
    
    class A:
      def __init__(self,name):
        self.__name = name
    
      def __get_name(self):
        return self.__name
    
      Name = __get_name()
    
    sa = A("Administrator")
    print(sa)

    但是总提示我__get_name()少了个参数给self…………这是为什么? 怎么解?

    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 孤单心事 普通会员 1楼

      The error message "missing 1 required positional argument: 'self'" usually occurs when you are trying to call a method or function on an object that does not have a "self" argument.

      For example, if you have a class "Person" with a method "greet", you should provide an instance of the class when calling the method:

      person = Person() person.greet()

      If you do not provide an instance of the class when calling the method, you will get the error message:

      TypeError: greet() missing 1 required positional argument: 'self'

      This means that you need to provide an instance of the class when calling the method, either by creating an instance yourself or by passing an instance as an argument.

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部