Python knowledge (3)

Source: Internet
Author: User
1. Class)

Python uses keywords to define classes. Each class has a _ init _ method to implement class initialization.

For Python classes, the first parameter of each method isSelf, Which indicates an instance of a class

For example, for a class,

A = ()

Equivalent

A. _ init _ ()

When defining a class, we are actually definingCustom factory function

Class A: def _ init _ (self, value = 325): Self. thing = valuedef howbig (Self): Return Len (self. thing) A = a ('hello') # Python actually runs. _ init _ (A, 'Hello') print (. howbig () # Python actually executes print (. howbig ())

Next, let's look at an example of class methods and class attributes.

Class athlete: def _ init _ (self, a_name, a_dob = none, a_times = []): Self. name = a_nameself.dob = a_dobself.times = a_timesccc = athlete ('ccc ') XT = athlete ('xt', '2017-08-30 ', [1, 2, 'A ', 'B']) print (CCC. name) # output cccprint (CCC. DOB) # output noneprint (CCC. times) # output [] print ('-----------') print (XT. name) # output xtprint (XT. DOB) # output 1985-08-30print (XT. times) # output [1, 2, 'A', 'B']

In python, the keywordNoneNull Value

2. class inheritance

As follows, Class A inherits the class list

class A(list):...

Let's look at a real example.

class A(list):def __init__(self, a_name):list.__init__([])self.name = a_namea = A('CCC')a.append('1')a.append('2')a.extend(['A', 'B', ['C', 'D']])for x in a:print(x)

Output result:

12AB['C', 'D']

3. class inheritance (Instance)

Now, define the class mylist (inherited from the List class) and add a method top3 () for it. This method converts the list content into uppercase, and then outputs the top three output items in the lexicographically ascending order.

Class mylist (list): # mylist inherits the List class def _ init _ (self, a_list = []): list. _ init _ ([]) # Call the initialization function self of the base class. extend (a_list) # mylist is also a list class, so you can call the extend method def top3 (Self) of the parent class: # Return the top three in the list (from small to large in Lexicographic Order, and all are converted to uppercase letters) return sorted (SET ([X. upper () for X in self]) [0: 3] # Note: here we use self to represent listmylist = mylist (['cc', 'A', 'B ', 'CA']) print (mylist. top3 ())

The output result is

['A', 'B', 'CA']

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.