Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

第3章 PasteFile类中使用 @property #39

Closed
xiaomiba0904 opened this issue Oct 29, 2016 · 2 comments
Closed

第3章 PasteFile类中使用 @property #39

xiaomiba0904 opened this issue Oct 29, 2016 · 2 comments

Comments

@xiaomiba0904
Copy link

您好请问用
@property def path(self): return get_file_path(self.filehash)
跟直接在init中写
self.path = get_file_path(self.filehash)
这2个用法有什么区别,第一个优势是什么,您为什么用第一种?谢谢

@dongweiming
Copy link
Owner

@xiaomiba0904 这个问题挺好。对这个例子来说,这2种用法的效果是一样的,但是我一般不会在初始化的时候添加不必要的、需要执行的语句,而是在需要的时候才去执行。这算是一种我的编程习惯吧,因为可能大部分的初始化 并不需要知道self.path是什么,如果放在__init__里面,那么就是无差别的都要执行一次,然而实际上用不到

@xiaomiba0904
Copy link
Author

谢谢解惑

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants