V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
levon
V2EX  ›  Python

看Python教程的一困惑

  •  
  •   levon · 2012-05-14 16:22:31 +08:00 · 3518 次点击
    这是一个创建于 4336 天前的主题,其中的信息可能已经有所发展或是发生改变。
    书中讲property的部分,举例代码
    __metaclass__ = type
    class Rectangle:
    ....def __init__(self):
    ........self.width = 0
    ........self.height = 0

    ....def getSize(self, size):
    ........self.width, self.height = size

    ....def setSize(self, size):
    ........return self.width, self.width

    ....size = property(getSize, setSize)

    ===
    #运行例子:
    >>>r = Rectangle()
    >>>r.width = 10
    >>>r.height = 5
    >>>r.size
    (10, 5)
    >>>r.size = 150, 100
    >>>r.width
    150

    =========================================
    1.这里的width和height叫什么东西(怎么称呼),我在运行个r.length时报错说:Rectangle instance has no attribute 'length',attribute和property有区别吗,区别是什么?

    2.真心看不出这个size属性有什么必要性,你说一个Rectangle有意义的表征width和height都知道,要size属性有何用,不是很多余吗

    对这个property很疑惑,我模糊觉得一个类似width和height的东东就可以替代,但既然有这东东,肯定有什么用途。
    10 条回复    1970-01-01 08:00:00 +08:00
    levon
        1
    levon  
    OP
       2012-05-14 16:33:56 +08:00
    分不清的名词有类特性, 类属性, 类成员
    英文好像分别是attribute, property, member
    cute
        2
    cute  
       2012-05-14 16:38:00 +08:00
    1. 这里的width和height叫属性,你根本没有定义length属性啊。
    2. 这个例子是property的用法. 你就接有没有干嘛啊。
    3. property是修饰(decorator)函数:size = property(取值方法 , 赋值方法),当对size做取值操作的时候,执行取值方法,赋值的时候执行赋值方法.
    cute
        3
    cute  
       2012-05-14 16:40:36 +08:00
    纠正一下property一个type类型
    levon
        4
    levon  
    OP
       2012-05-14 16:43:42 +08:00
    @cute
    1. 用length,我是故意要引出attribute这个单词的
    2. 能不能举个使用property比较实在的例子?
    keakon
        5
    keakon  
       2012-05-14 16:44:58 +08:00   ❤️ 1
    1. 属性。Attribute 和 property 都翻译成属性,在 Python 中,attribute 是真实存在的一个变量,property 可以是一个函数的计算结果。

    2. 必要性就是他想教你这个,就写上了。

    使用 property 时,你就当成不需要写括号的方法调用就行了。用途就是你的类给别人使用时,比调用方法看上去简便。
    比如 article.author.name 就比 article.find_author().full_name() 看上去简便,尽管内部的逻辑都是一样的。
    cute
        6
    cute  
       2012-05-14 16:49:31 +08:00   ❤️ 1
    import time
    class TestProperty:
    def __init__(self):
    self.now = time.time()

    @property
    def now_human(self):
    return time.strftime('%Y-%m-%dT%H:%M:%S', time.gmtime(self.now))

    t = TestProperty()
    print t.now
    print t.now_human

    会使代码变得优雅和方便
    levon
        7
    levon  
    OP
       2012-05-14 17:45:08 +08:00
    @cute 我依稀感觉到实用性 ,就是一些简单操作干脆就在类里设计好做成属性。是不是可以这样觉得。那种无需任何操作的就设计成attribute 如你例子中的now
    thedevil7
        8
    thedevil7  
       2012-05-14 17:48:23 +08:00
    其实, 我觉得如果说初学, 只要会用, 能写出可以运行, 可以满足需要的代码就可以了吧.. 不需要过分深究, 学到一定程度之后再深入.
    cute
        9
    cute  
       2012-05-14 21:35:25 +08:00
    @levon 可以的。
    bhuztez
        10
    bhuztez  
       2012-05-14 22:03:19 +08:00
    我是吐槽LZ的,碰到一点语法糖,就要个问为什么,我不知道你是在学语言呢,还是学怎么设计语言呢。语法糖这玩意儿,设计语言的人觉得有用就加进去了,你觉得有用就用,你觉得没用就别用,你不用又没人逼你用。前一帖非要用class-based的概念去套prototype-based的python,这帖又要对getter/setter存在的意义进行质疑。你要么干脆什么语法糖都不要,就去用McCarthy论文里提出的原始的Lisp,或者机器码就行了。

    另外,Python和优雅真是一点关系都没有。Python自命大巧若拙,其实和那些自以为精巧的语言相比真的好不了多少。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3769 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 10:37 · PVG 18:37 · LAX 03:37 · JFK 06:37
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.