Python进阶(十二)浅谈python中的方法_person‘ object has no attribute ‘score

这里分享一份由字节前端面试官整理的「2021大厂前端面试手册」,内容囊括Html、CSS、Javascript、Vue、HTTP、浏览器面试题、数据结构与算法。全部整理在下方文档中,共计111道

HTML

  • HTML5有哪些新特性?

  • Doctype作⽤? 严格模式与混杂模式如何区分?它们有何意义?

  • 如何实现浏览器内多个标签页之间的通信?

  • ⾏内元素有哪些?块级元素有哪些? 空(void)元素有那些?⾏内元 素和块级元素有什么区别?

  • 简述⼀下src与href的区别?

  • cookies,sessionStorage,localStorage 的区别?

  • HTML5 的离线储存的使用和原理?

  • 怎样处理 移动端 1px 被 渲染成 2px 问题?

  • iframe 的优缺点?

  • Canvas 和 SVG 图形的区别是什么?

JavaScript

开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】

  • 问:0.1 + 0.2 === 0.3 嘛?为什么?

  • JS 数据类型

  • 写代码:实现函数能够深度克隆基本类型

  • 事件流

  • 事件是如何实现的?

  • new 一个函数发生了什么

  • 什么是作用域?

  • JS 隐式转换,显示转换

  • 了解 this 嘛,bind,call,apply 具体指什么

  • 手写 bind、apply、call

  • setTimeout(fn, 0)多久才执行,Event Loop

  • 手写题:Promise 原理

  • 说一下原型链和原型链的继承吧

  • 数组能够调用的函数有那些?

  • PWA使用过吗?serviceWorker的使用原理是啥?

  • ES6 之前使用 prototype 实现继承

  • 箭头函数和普通函数有啥区别?箭头函数能当构造函数吗?

  • 事件循环机制 (Event Loop)

    self.name = name
    self.score = score
def get\_grade(self):
    return 'A'

p1 = Person(‘Bob’, 90)
print p1.get_grade

=> <bound method Person.get_grade of <__main__.Person object at 0x109e58510>>

print p1.get_grade()

=> A


也就是说,p1.get\_grade 返回的是一个函数对象,但这个函数是一个绑定到实例的函数,p1.get\_grade() 才是方法调用。


因为方法也是一个属性,所以,它也可以动态地添加到实例上,只是需要用 types.MethodType() 把一个函数变为一个方法:



import types
def fn_get_grade(self):
if self.score >= 80:
return ‘A’
if self.score >= 60:
return ‘B’
return ‘C’

class Person(object):
def __init__(self, name, score):
self.name = name
self.score = score

p1 = Person(‘Bob’, 90)
p1.get_grade = types.MethodType(fn_get_grade, p1, Person)
print p1.get_grade()

=> A

p2 = Person(‘Alice’, 65)
print p2.get_grade()

ERROR: AttributeError: ‘Person’ object has no attribute ‘get_grade’

因为p2实例并没有绑定get_grade


给一个实例动态添加方法并不常见,直接在class中定义要更直观。


由于属性可以是普通的值对象,如 str,int 等,也可以是方法,还可以是函数,大家看看下面代码的运行结果,请想一想 p1.get\_grade 为什么是函数而不是方法:




### 总结

我在成长过程中也是一路摸爬滚打,没有任何人的指点,所以走的很艰难。例如在大三的时候,如果有个学长可以阶段性的指点一二,如果有已经工作的师兄可以告诉我工作上需要什么,我应该前面的三年可以缩短一半;后来去面试bat,失败了有5、6次,每次也不知道具体是什么原因,都是靠面试回忆去猜测可能是哪方面的问题,回来学习和完善,当你真正去招人的时候,你就会知道面试记录是多么重要,面试官可以从面试记录里看到你的成长,总是去面试,总是没有成长,就会被定义为缺乏潜力。

**[开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】](https://bbs.csdn.net/forums/4304bb5a486d4c3ab8389e65ecb71ac0)**

![image](https://img-blog.csdnimg.cn/img_convert/67c88deb325e024b944de9ae671274d0.webp?x-oss-process=image/format,png)
![image](https://img-blog.csdnimg.cn/img_convert/3e52dfbdba89a024d9f7a1055e1d441d.webp?x-oss-process=image/format,png)

dn.net/forums/4304bb5a486d4c3ab8389e65ecb71ac0)**

[外链图片转存中...(img-Msa3beFi-1715792253860)]
[外链图片转存中...(img-c5LOvmbc-1715792253861)]

  • 25
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
As an AI language model, I cannot see your code, so I can only provide a general solution. This error occurs when you try to access an attribute called "retranslateUi" that does not exist in the Ui_MainWindow object. This attribute is typically generated by the PyQt5 or PySide2 UI compiler when you create a new UI file in Qt Designer and convert it to Python code. To fix the error, you need to make sure that your UI file has been properly converted to Python code and that the "retranslateUi" method is defined in the class that inherits from Ui_MainWindow. Here are some steps you can take: 1. Make sure that you have converted your UI file to Python code using the PyQt5 or PySide2 UI compiler. You can do this by running the following command in your terminal or command prompt: ``` pyuic5 <ui_file_name>.ui -o <python_file_name>.py ``` Replace <ui_file_name> with the name of your UI file (including the .ui extension) and <python_file_name> with the name of the Python file you want to generate (including the .py extension). 2. Open the Python file that was generated by the UI compiler and look for the class that inherits from Ui_MainWindow. This class should have a method called "retranslateUi" that is used to translate the text of your UI elements to different languages. 3. If the "retranslateUi" method is missing, you can add it manually to your class definition. Here's an example of what the method should look like: ``` def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "My Application")) self.pushButton.setText(_translate("MainWindow", "Click Me!")) # Add more translations for your UI elements here ``` Make sure to replace "MainWindow" with the name of your main window object, and add translations for all of your UI elements that need to be translated. 4. Save your Python file and run your application again. The "retranslateUi" method should now be called correctly and your UI elements should be translated properly. I hope this helps! Let me know if you have any other questions.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值