Python property() FunctionPython Functions


The property() function is a built-in function that is used to create property of a class.

Example

#!/usr/bin/python3
# -*- coding: UTF-8 -*-

class Student(object):

    def get_score(self):
        return self._score

    def set_score(self, value):
        if not isinstance(value, int):
            raise ValueError('score must be an integer!')
        if value  100:
            raise ValueError('score must between 0 ~ 100!')
        self._score = value

s = Student()
s.set_score(79)
print(s.get_score())

Output:

79

Syntax

class property([fget[, fset[, fdel[, doc]]]])

Parameters

fget — A function used to get attribute values.
fset — A function used to set property values.
fdel — Delete attribute value functions.
doc — Attribute Description Information.

Return Value

It returns a property attribute from the given getter, setter, and deleter.

© 2021 Programtips.org, all rights reserved. Privacy Policy | Contact Us