Language Features of Python

1. Passing Python function parameters

Example 1: a = 1

def fun(a):

    a=2

fun(a)

print a #1

Example 2: a = []

def fun(a):

    a.append(1)

fun(a)

print a #1

The type remembered here belongs to the object, and the object is divided into mutable and immutable, mutable list, dict, immutable is string, number, tuple.

A reference is passed to a function, and the function automatically copies a reference. The reference inside the function has nothing to do with the outside. In the first example the function points the application to an immutable object, and the result is different inside the function than outside. The second example function points a reference to a mutable object, which is modified directly in memory.

2.@classmethod 和 @staticmethod

In Python, there are static methods, instance methods and class methods.

def foo(x):

    print "excuting foo(%s)"%(x)

class A(object):

    def foo(self,x):

        print "excuting foo(%s,%s)"%(self,x)

   @classmethod

    def class_foo(cls,x):

        print "excuting class_foo(%s,%s)"%(cls,x)

 @staticmethod

    def static_foo(x):

        print "excuting static_foo(%s)"%x

a=A()

Instance a can call static methods, class methods and instance methods Class A can call static methods and class methods, but not instance methods

First of all, we must understand self and cls. This self and cls are a binding to an instance or class. The general function foo(x) can be called at will. It has nothing to do with any instance or class, and the invocation of instance methods cannot be separated from it. Instance, each call must pass the instance itself to the function, a.foo(self, x) is equal to a.foo(a,x), a.foo(x) is actually a.foo(a,x), The class method is the same, except that it passes the class instead of the instance, A.foo(x).

3. Class variables and instance variables

class Person():

    name="aaa"

p1=person()

p2=person()

p1.name="bbb"

print p1.name #bbb

pring p2.name #aaa

print Person.name #aaa

p1.name, the class variable was called at the beginning, but the scope of the instance changed the reference of the class variable into an instance variable, so self.name no longer points to the class variable.

Take a look at the following example:

class Person ():

    a=[]

p1=Person(0

p2=Person()

p1.name.append(1)

print p1.a #[1]

print p2.a#[1]

print Person.a#[1]


3. What is the difference between single underscore and double underscore in python?

_foo(), is a convention that refers to variable privatization,

_foo()_ is a constraint used to distinguish other users' own names to prevent conflicts.

4. What is the difference between _new_ and _init_?

_new_ is a static method, _init_ is an instance method,

The _new_ method returns an instance, the _init_ method returns nothing,

The _init_ method is called only after _new_ returns an instance

5. Singleton mode (import version)

class My_singleton(objec):

    def func(self):

        pass

mysingleton = My_singleton()

#to use

from mysingleton.py import My_singleton;

my_singleton.foo()

6. Write a function that inputs a string and returns the result in reverse order

1. Use the method that comes with the string: def reverse_text(text='abcdef')

    return [::-1]

2. Turn the string into a list and use the reverse method of the list

def string_reverse(text='abcdef'):

    new_text=list(text)

    new_text.reverse()

    return "".join(new_text)

print string_reverse;

3. Create a new list and take it forward from the back of the list

def string_reverse(text='abcdef'):

    list=[]

    for i in range(1,len(list)+1):

        list.append(text[-i])

return ''.join(list)


4. Merge the following two lists in ascending order, and remove duplicate elements

list1 = [ 2 , 3 , 8 , 4 , 9 , 5 , 6 ]
 
list2 = [ 5 , 6 , 10 , 17 , 11 , 2 ]

list3 = list1 + list2
 
print set (list3)

5. Will the following code report an error? The code will return a [] and will not report IndexError
list = [ 'a' , 'b' , 'c' , 'd' , 'e' ]

print list[10:]  


The new default list is created only once when the function is defined, and the list that is not specified in the function is actually just a list of consents to operate on.





























        











Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324626067&siteId=291194637