On the common doubts and answers of Python novice

Source: Internet
Author: User
1 Lambda functions

The function format is the lambda keys:express anonymous function lambda is an expression function that accepts the keys parameter and returns the value of the expression. So no return, no function name, often used in functions that require key parameters, such as sorted.

2 tuples (), which are distinguished by commas, not parentheses. For example, a tuple of an element is often written as (12), and in fact he is interpreted as a single element 12. The correct wording should be (12), followed by a comma after the element.

3 Module Import. Like what

Import Random
Print Random.choice (range (10))

And

From random import choice
Print Choice (range (10))

Novice will have a misunderstanding, the second method only imports a function, but not the entire module import, this is wrong. The entire module has actually been imported, but the reference to that function has been saved. So from-import this syntax does not result in performance differences, nor does it save memory.

4 When you have many module, such as hundreds of, you might want to use a import too cumbersome, there is no easy way? the answer is that the modules are organized into a package. In fact, the module is placed in a directory, and then add a __init__.py file, Python will think of it as a package, using the inside of the function can be accessed in dotted-attribute way.

5 Parameter passing mutable object is referenced, immutable object is passed value. So what objects are mutable and what is immutable. All Python objects have three properties: a type, an identifier, and a value, if the value is mutable, or a mutable object, if the value is immutable, it is a non-mutable object. Like numbers, strings, tuples are immutable objects, and the rest of the list, dictionaries, classes, class instances, and so on are mutable objects.

The understanding of the 6 iterator is the container object that implements the iterator protocol. implement an iterator yourself, with the __iter__ () method in the class that returns an object. This object should have the __next__ () method and return the stopiteration exception in the appropriate place in the next method. Iterators are not used very often, so don't worry too much. An alternative is the generator.

Class Myiterator (object): "" "" ""  docstring for Myiterator "" "  def __init__ (self, num):    self.num = num  def _ _iter__ (self):    return to self;  def __next__ (self):    if self.num <= 0:      raise stopiteration;        Self.num-= 1;    Return self.num;for Myiterator (5):  print (each);-> results

7 Generator. The yield statement in the function turns it into a generator. After the yield statement is met, the context is saved and the function exits.

Note: There is no return statement in the generator.

def fun2 (num):  print ("Start generator");  while (num>0):    yield num;    Num-=1;a=[each for each in fun2 (5)]print (a);-> results start generator[5, 4, 3, 2, 1]

In the course of learning, mistakes are unavoidable. If you're having trouble reading, you don't understand or have questions.

This article on the new Python common doubts and answers are small series to share all the content, I hope to give you a reference, but also hope that we support the script home.

  • 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.