US-depth learning (Zhang Yuhong) - Chapter IV Life is short I use python

1 function parameters

(1) collection parameters: an asterisk * plus mode parameter name indicates the number of variable arguments of this function, the n may be 0 possible.

DEF varParaFun (name, * param):
     Print ( ' positional parameters are: ' , name)
     Print ( ' collection parameters: ' , param)
     Print ( ' first collection parameters: ' , param [0])

varParaFun ( ' data beauty ' , ' Hello ' , 123, ' ABC ' )

# Positional parameters are: United States of data 
# collection parameters are: ( 'Hello', 123, 'ABC') 
# The first parameter is a collection: hello

(2) collection parameters: with two asterisks (**) to calibrate the variable parameter, the variable parameter packaged as a dictionary. It shows that the variable parameter is the dictionary elements. Function call using a method such as is required in the form arg I = the value l.

def varFun(**param):
    if len(param)==0:
        print('None')
    else:
        print(param)
    
varFun(a='beauty',b=2)

#{'a': 'beauty', 'b': 2}

2 pass by value or reference - pass by reference

  If the argument passed is immutable object, in order to maintain its "immutable" attributes, internal functions have reconstructed a copy argument, i.e., a copy of the argument and parameter arguments provided by the calling function in memory The actual points in different positions. Accordingly, modifications of the function parameter arguments will not have any impact, as by value and therefore appears in the results. Function changes only parameter to generate a new thing, again designated by the same name.

def foo1(a):
    A = A + ( ' D ' , ' E ' )
     Print ( ' call in: ' , A)
    
tuple1 = ( ' A ' , ' B ' , ' C ' )
 Print ( ' before the call: ' , tuple1)
foo1(tuple1)
Print ( ' after calling: ' , tuple1)


def foo2(a):
    a.append ( ' variable object ' )
     Print ( ' call in: ' , A)

List1 = [ ' A ' , l, 2,3 ]
 Print ( ' before the call: ' , list1)
foo2(List1)
Print ( ' after calling: ' , List1)

# Before calling: ( 'a', 'B', 'C') 
# among call: ( 'a', 'B', 'C', 'D', 'E') 
# called after: ( 'a ',' b ',' c ')

# Before calling: [ 'a', 1, 2, . 3] 
# being invoked: [ 'a', 1, 2, 3, ' variable object'] 
# after the call: [ 'a', 1, 2, 3, 'variable object']

Object-oriented and process-oriented 3

 POP (Procedure Oriented Programming) Algorithms + Data Structures = Programs, design thinking is "How to do", how refers to function.

 OOP (Object Oriented Programming) + messaging program objects = = (+ Method Object) + messaging idea is "Who to do", who refers to an object refers to the object, various methods.

4 python Object-oriented - class

  (1) Class: class data members can be roughly divided into two categories: data belonging to the object data members and member of the class. Belonging to the object data members defined in the main __init __ () are, independently of each other with different objects in a class definition, and the data for all the members belong to the class of shared objects. __ data members or method to start, belonging to private, members only such internal use within the class can not be inherited, but can be accessed through the "object name ._ class name __xxx" way. Starting with _ belong to protected members only for themselves and their subclasses open permissions, can not "from module import *" way of introduction. "__Xxx__" form part of a special python system from members defined as __init __, __ del__. Method in class must contain self and is the first parameter. 

class Person:
    height = 162                     # data members defined in the class
    
    def __init__(self,name,age,weight):
        the self.name = name           # data members defined object properties 
        self.age = Age
        Self. __weight = weight     # private data members, can only be accessed by the public member functions within the class
        
    DEF Speak (Self):               # public member functions 
        Print ( ' . My name is% s, my age is% d, Height% dcm, my weight was DKG% ' % (self.name, self.age, Person.height, Self. __weight ))
    
people=Person('PinkFox',23,46)
people.speak ()                     # call public member function, also known as the access method 

Person.height = 165                  # data members belong to the class of re-assignment 
people.name = ' RedRabbit '            
people.age = 24-                      # to access the properties and assign 
people.speak ( )

# My name PinkFox, my age is 23, my height is 162cm, my weight is 46KG. 
# I'm RedRabbit, my age is 24, my height is 165cm, my weight is 46KG.

  (2) python in succession: inheritance aims to achieve code reuse, namely, the implementation of a subclass of "ism" from the parent class to the existing mature function. Parent, also known as the base class, superclass, subclass, also known as the derived class.

class Students(Person):
    School = ' Hogwarts '
    
    def __init__(self,name,age,weight,grad):
        self.grade = Grad
      # call the parent class constructor, initialize the data members of 
        the Person. __init__ (Self, name, Age, weight)
     DEF Speak (Self):
         Print ( ' My name is% s, my age is% d, I height% dcm, I read% d year in S%. ' % (self.name, self.age, Person.height, Students.school, self.grade))
        
stu=Students('PinkFox',13,36,5)
Person.height=158
stu.speak()

# My name PinkFox, my age is 13, my height is 158cm, I read in the 5-year Hogwarts.

Life is happy together again and again, again and again mingled with sad parting. We are not the best time to meet, but you have met the best of times. - Adapted from "Teacher, good."

 



Guess you like

Origin www.cnblogs.com/DHuifang004/p/11067091.html