1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Python 属性のエイリアス

Posted at

property() をデコレータではなく、ただの関数として使う。
SQLAlchemy で使われている。

from operator import attrgetter


class A:
    def __init__(self):
        self.foo = "foo"

    f = property(attrgetter("foo"), doc="An alias for the :attr:`.foo` attribute.")


if __name__ == "__main__":
    help(A.f)

    a = A()
    print("A.foo:", a.foo)
    print("A.f:", a.f)

実行例。

$ python sample_alias.py
Help on property:

    An alias for the :attr:`.foo` attribute.

A.foo: foo
A.f: foo
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?