Introduction to Python property built-in functions

Source: Internet
Author: User

The basic function of a function property is to access the methods in the class as attributes, as described in an interesting example:

If there is a cat, it forgets what it likes to eat, let's see how we can fix it.

Original code: #运行环境 python2.7.10

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Classcat (object): def__init__ (Self,food): Self.food=food defeat (self): Returnself.food def             Say (self): if ' Im_func ' Indir (self.eat): print "I forgot what to Eat,mybe%s"%self.food Else:   Print "Miao...,i want to eat fish!" if__name__== "__main__": Tim=cat (' Stone ') Tim.say ()

After running, the cat said "I forgot to Eat,mybe Stone"

Looks like it's a real problem.

Let's make a little change:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 Classcat (object): def__init__ (Self,food): Self.food=food @property defeat (self): returnself.         Food Defsay (self): if ' Im_func ' Indir (self.eat): print "I forgot what to Eat,mybe%s"%self.food   Else:print "Miao...,i want to eat fish!" if__name__== "__main__": Tim=cat (' Stone ') Tim.say ()
This time the cat remembered.

"Miao...,i want to eat fish!"

Looks like the @property on the decorator.

If we remove the @property

Add a line of code behind

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #!/usr/bin/python #-*-utf-8-*-Classcat (object):    def__init__ (self,food):        self.food=food     @property    defeat (self):        returnself.food        defsay (self):        if ' Im_func ' Indir (self.eat):            print "I forgot to Eat,mybe%s"%self.food   &nbsp ;    else:            print "Miao...,i want to eat fish!"     If__name __== "__main__":    tim=cat (' Stone ')    tim.say ()    try:         tim.eat= "is fish deliciout?"        printtim.eat    except :        print "^^, Fish is so nice"               &nb sp;<div>                  </div>

Output:

I forgot to Eat,mybe Stone
is Fish deliciout?


Now let's add the decorator @property.

Output:

Miao...,i want to eat fish!
^^, Fish is so nice


Summing up: the addition of @property modified method becomes a data attribute, is no longer a method, as follows:

Class Cat (object):
def __init__ (Self,food):
Self.food = Food
# @property
Def eat (self):
Return Self.food
Def say (self):
Print dir (self.eat)
If ' Im_func ' in Dir (self.eat):
Print "I forgot to Eat,maybe%s"%self.food
Else
Print "Meow ..., I want to eat fish!"

if __name__ = = ' __main__ ':
Tim = Cat (' Stone ')
Tim.say ()
Try
Tim.eat = "is fish delicious?"
Print Tim.eat
Except
print ' ^^, fish is so nice! '

The operating result is:

Meow ..., I want to eat fish!
^^, fish is so nice!

If this is the following code:

Class Cat (object):
def __init__ (Self,food):
Self.food = Food
# @property
Def eat (self):
Return Self.food
Def say (self):
Print dir (self.eat)
If ' Im_func ' in Dir (self.eat):
Print "I forgot to Eat,maybe%s"%self.food
Else
Print "Meow ..., I want to eat fish!"

if __name__ = = ' __main__ ':
Tim = Cat (' Stone ')
Tim.say ()
Try
Tim.eat = "is fish delicious?"
Print Tim.eat
Except
print ' ^^, fish is so nice! '

The operating result is:

I forgot to Eat,maybe stone
is fish delicious?

If this is the following code:

Class Cat (object):
def __init__ (Self,food):
Self.food = Food
@property
Def eat (self):
Return Self.food
Def say (self):
Print dir (self.eat)
If ' Im_func ' in Dir (self.eat):
Print "I forgot to Eat,maybe%s"%self.food
Else
Print "Meow ..., I want to eat fish!"

if __name__ = = ' __main__ ':
Tim = Cat (' Stone ')
Tim.say ()
Try
TIM.EAT1 = "is fish delicious?"
Print TIM.EAT1
Except
print ' ^^, fish is so nice! '


The operating result is:

Meow ..., I want to eat fish!
is fish delicious?

Here though Tim. Eat is no longer a method, but a data attribute, but since we have not modified Tim in the main function. Eat but modified the TIM.EAT1, so get the result is the above results, hehe

The main thing is that after the property modification function is no longer a function, the method is no longer a method, but a data attribute,

Introduction to Python property built-in functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.