Head Frist Python Reading notes sixth customizing data Objects

Source: Internet
Author: User

Bullet Points:

    • Define Class

Python is similar in class to JavaScript (regrets not looking at JS), only one "method is shared, and property is not shared"

classathletelist:def __init__( Self, a_name,a_dob=none,a_times=[]):Self.name=a_name Self.dob=A_dob self.times=a_timesdeftop3 (self):returnSorted (Set ([Float (sanitize (item)) forIteminchSelf.time]) [0:3]    defadd_time (Self, Time): Self.times.append (Time)defadd_times (Self, Times): Self.times.extend (Times)

1. The __init__ method is equivalent to a constructor function

2. The first parameter of each method, self, refers to the current instance object

Python can, of course, inherit it.

classathletelist (list):def __init__(self,a_name,a_dob=none,a_times=[]): list. __init__ ([]) self.name=a_name Self.dob=a_dob self.extend (a_times)deftop3 (self):returnSorted (Set ([Float (sanitize (item)) forIteminchSelf]) [0:3]    defAdd_time (self,time): Self.append (Time)defAdd_times (self,times): Self.extend (Times)

Similar to the inheritance of other languages, except that the syntax is not the same, and the constructor of the inherited class needs to be called in the constructor

Head Frist Python Reading notes sixth customizing data Objects

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.