Python (face question II)

Source: Internet
Author: User

What is the difference between a class method, a class instance method, and a static method in 1.python?

Difference:

    • Both class and static methods can be called by class and class instances, and class instance methods can only be called by class instances
    • The implied invocation parameter of a class method is a class, and the implied invocation parameter of a class instance method is an instance of the class, and the static method does not implicitly invoke the argument

Examples of Use:

Python code:

1 classA (object):2     deffoo (self,x):3     #class instance method4         Print "executing foo (%s,%s)"%(self,x)5  6 @classmethod7     defClass_foo (cls,x):8     #class Method9         Print "executing Class_foo (%s,%s)"%(cls,x)Ten   One @staticmethod A     defStatic_foo (x): -     #Static Methods -         Print "executing static_foo (%s)"%x

Calling methods

1A =A ()2A.foo (1)//Print: Executing foo (<__main__. A Object at 0xb77d67ec>,1)3   4A.class_foo (1)//executing Class_foo (<class '__main__. A'>,1)  5A.class_foo (1)//executing Class_foo (<class '__main__. A'>,1)  6   7A.static_foo (1)//executing Static_foo (1)  8A.static_foo (1)//executing Static_foo (1)

Similarities and differences of xrange and range in 2.python

The xrange usage is exactly the same as the range, but the difference is that xrange generates not a list, but a generator. In order to generate a large number sequence, using xrange will be much better than range performance, because there is no need to open up a large amount of memory space.

Range generates a list object directly:

1 >>> a = range (0, 2)print  type (a)3'  List'>4print  a5 [0, 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, 39, 40, 41 , 45, 46, 47, 48, 49]

Xrange does not generate a list directly, but instead returns one of the values for each call:

1 >>> a = xrange (0, 2)print  type (a)3'  Xrange'>4print  a5 xrange (50)

3. Please use Python to implement the following C code question mark expression:

int n = a>b? (A-B): 0

Requires the simplest way to implement it.

1  and or 0 2 Print N 3 0 4  and or 0 5 Print N 6 -1

What is the implementation mechanism of 4.python multithreading? Under what circumstances can the use of multithreading significantly improve program efficiency?

5. Write the regular expression to extract the link address from a string, such as the following string <a href=http://www.mianwww.com/html/category/it-interview /flex>Flex</a>

The link that needs to be extracted is "Http://www.mianwww.com/html/category/it-interview/flex"

1 >>> href = re.findall (r"<a.*?href= (http://.*?) >", String)2print  href3 [' Http://www.mianwww.com/html/category/it-interview/flex ']

Some people say back. *. and? No, I tried.

1 >>> href = re.findall (r"<a.*?href= (http://*) >", String)  2print  href3[]4 >>> href = re.findall (r"  <a.*?href= (http://*?) >", String)5print  href6 []

6. Reverses the string consisting of a word and an indefinite number of spaces, requiring the alphabetical order of the words to be the same. such as: "I am   a      boy" reversed into "boy      a   am I".

1 Import Re 2 " I am   a      boy"3". Join (Re.split (R'(\s+) ', String] [:: -1])4print  revwords5 Boy      a   am I

Not to be continued ...

Python (face question II)

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.