[Reading] Python learning manual-attribute Management 1, python learning Manual

Source: Internet
Author: User

[Reading] Python learning manual-attribute Management 1, python learning Manual
Property Management-Features

Generally, development does not need to care about attribute implementation. For tool building, understanding this is helpful for API flexibility.
In most cases, attributes are in the object itself, or inherit from a class derived from the object. ---- Python learning Manual

Property

Property (fget = None, fset = None, fdel = None, doc = None)-> property attribute
Fget is a function to be used for getting an attribute value, and likewisefset is a function for setting, and fdel a function for del 'ing, an attribute.
You can use functions or decorator.

#! /Usr/bin/env python #-*-coding: UTF-8-*-# python2.7x # property. py @ 2014-07-26 # author: orangleliuclass Person (object): def _ init _ (self, name): self. _ name = name def getName (self): print 'fetch .... 'Return self. _ name def setName (self, value): print 'change... 'self. _ name = value def delName (self): print 'remove .... 'del self. _ name # You can also use the name = property (getName, setName, delName, "name property docs") bob = Person ('bob') print Bob. nameprint Person. name. _ doc _ bob. name = 'bob' print bob. namedel bob. name # print bob. name''' is not as good as you think # fetch when the class does not inherit the object .... bobname property docsbobset del is not used. # Bobname property docschange when the class inherits objects... fetch .... bobremove .... '''


The example in the book does not inherit objects. The version 2.7 is different from the result in the book. Objects must be inherited to achieve the expected results.

Default operation for adding Properties

Here we use the decoration method. We only need to assign values to perform a default operation. We can see that we can use the attribute method to call functions by default to process attributes. G. value instead of g. valueXX ()

#! /Usr/bin/env python #-*-coding: UTF-8-*-# python2.7x # property. py @ 2014-07-26 # author: orangleliuclass GetSquare (object): def _ init _ (self, num): self. value = num @ property def value (self): return self. _ value @ value. setter def value (self, num): self. _ value = num ** 2 ''' From the above we can see that some attributes of the set property value are acted by someone, sometimes it is necessary to ''' g = GetSquare (4) print g. valueg. value = 10 print g. value


In fact, this method is similar to the method in javabean.


After reading the python learning manual, How can I improve my python programming skills?

1) Python is a very easy-to-use language, so you must be able to learn it carefully.
2) If you want to improve any language, you can only continue to practice, practice, and practice. Practice makes perfect.
3) at the beginning, you can find a book with examples and follow the examples in the book to practice and understand. After getting familiar with such a language to a certain extent, you can consider how to use it to work, for example, which jobs can be made simple using python. From simple to complex, step by step.
Learning programming is the same as practicing martial arts in novels. algorithms are just a matter of practice. How can I keep improving my skills when I read the cheats all day without practicing them? To a certain extent, K people can be trained and improved in practice. This process should be good at thinking and summarizing (understanding ). I'm afraid I can't beat it, but I'm sure I can only beat Luo (how can I upgrade my experience without playing the blame ?). You can become a master only when you take the shot. Good luck!

Python learning manual version 4

The text version only has English and Chinese characters.
The scan version is available in both Chinese and English. If you need it, I can send it to you.
It is recommended that you read the English version more. Some of the translation meanings are incorrect, and the English version is used to ask questions on Foreign Forums.

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.