[PYTHON] Attribut

Attributimplementierung

Implementieren Sie mit einfachen öffentlichen Attributen

python


class Book:
    def __init__(self, title):
        self.title = title
        self.page = 0
        self.isbn10 = ''

def main():
    b = Book('python book')
    b.page = 100
    b.page += 1
    print b.page

if __name__ == '__main__':
    main()

Verwenden Sie Dekorateure, wenn ein besonderes Verhalten erforderlich ist

python


class Book(object):
    def __init__(self, title):
        self.title = title
        self.page = 0
        self.isbn10 = ''

class BookEx(Book):
    def __init__(self, title):
        super(BookEx, self).__init__(title)
        self._page = 0

    @property
    def page(self):
        return self._page

    @page.setter
    def page(self, page):
        # something do need.
        self._page = page

def main():
    b = Book('python book')
    b.page = 100
    b.page += 1
    print b.page

if __name__ == '__main__':
    main()

Recommended Posts

Attribut
Attribut
Python-Attribut-Alias