【Python】NameError: name ‘self‘ is not defined

NameError: name 'self' is not defined


最近开发Python包,遇到一个“NameError: name ‘self’ is not defined”问题。
在执行

class Tasdfa:
    
    def __init__(self,prompt='asdfa',newline=False):
        self.newline=newline
        self.prompt=prompt
        
    def __getattr__(self,prompt=self.prompt,newline=self.newline):
        if self.newline:
            print('')
        print(self.prompt)

的时候,总在“def __getattr__(self,prompt=self.prompt,newline=self.newline):”处报错:

NameError: name 'self' is not defined

错误原因是不能在类成员函数的参数里带self

self是占位用的变量。调用一个类实例的成员函数时,解释器会在实参表前添加一个传给成员函数的变量,这个变量就是类实例本身。
有时,成员函数需要对本身(即自己这个类实例)操作,所以在形参表的第一项用self占位(所以其实可以用任何的变量名,完全可以用诸如this等等)。
但是,在形参表上下文中self还没有定义:形参表的self是形参还不能被调用,必须等到成员函数内部,self指向了一个实参之后,才可以使用self的方法。

改成

class Tasdfa:
    
    def __init__(self,prompt='asdfa',newline=False):
        self.newline=newline
        self.prompt=prompt
        
    def __getattr__(self):
        if self.newline:
            print('')
        print(self.prompt)

后,正确运行。

  • 11
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
It seems that with each new book the scope gets fuzzier and less precise. When I started writing Test-Driven Java Development the scope of the whole book was done in advance. I had a team working with me. We defined the index and a short description of each chapter. From there on we worked on a schedule as most technical authors do. Then I started writing the second book. The scope was more obscure. I wanted to write about DevOps practices and processes and had only a very broad idea what will be the outcome. I knew that Docker had to be there. I knew that configuration management is a must. Microservices, centralized logging, and a few other practices and tools that I used in my projects were part of the initial scope. For that book I had no one behind me. There was no team but me, a lot of pizzas, an unknown number of cans of Red Bull, and many sleepless nights. The result is The DevOps 2.0 Toolkit: Automating the Continuous Deployment Pipeline with Containerized Microservices. With the third book, the initial scope became even more obscure. I started writing without a plan. It was supposed to be about cluster management. After a couple of months of work, I attended DockerCon in Seattle where we were presented with the new Docker Swarm Mode. My immediate reaction was to throw everything I wrote to trash and start over. I did not know what will the book be about except that it must be something about Docker Swarm. I was impressed with the new design. Something about Swarm ended up being The DevOps 2.1 Toolkit: Docker Swarm: Building, testing, deploying, and monitoring services inside Docker Swarm clusters. While working on it, I decided to make DevOps Toolkit Series. I thought that it would be great to record my experiences from different experiments, and from working with various companies and open source projects. So, naturally, I started thinking and planning the third installment in the series; The DevOps Toolkit 2.2. The only problem is that, this time, I truly don't have a clue what will it be about. One idea was to do a deep comparison of different schedulers (for example, Docker Swarm, Kubernetes, and Mesos/Maraton). The another was to explore serverless. Even though it is a terrible name (there are servers, we just don't manage them), it is a great subject. The ideas kept coming but there was no clear winner. So, I decided not to define the scope. Instead, I defined some general objectives.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值