Get the App
SLTechnology News&Howtos  ›  Servers  › 

Parameter transfer of Python function and detailed explanation of variable scope

Shulou Source: shulou.com Published: 2022-06-01 10:19:02 09月12日 Update

This article mainly introduces the "Python function parameter transfer and variable scope detailed explanation", in the daily operation, I believe that many people in the Python function parameter transfer and variable scope detailed interpretation of the problem there are doubts, the editor consulted all kinds of data, sorted out a simple and useful method of operation, hope to answer the "Python function parameter transfer and variable scope detailed explanation" doubt helpful! Next, please follow the editor to study!

1. Pass multiple parameters

Use the * sign to indicate that the function accepts multiple arguments

> def f (* args): print args > f (1) (1,) > > f (1) (1,) > f (1) (1) (1)

two。 Pass dictionary parameters

Use the * * sign to indicate that the dictionary parameter is passed

> def F1 (* * args): print args > F1 (axi1moving baccalaure2) {'aqu: 1, 'baked: 2}

3. Pass the function name as an argument

# coding=utf-8 # is used to support Chinese characters. There can be no spaces before and after the = sign. # minmax.py def minmax (test,*args): res=args [0] for arg in args [1:]: # copy of the content in the circular args if test (arg,res): res=arg return res def lessthan (xMagery): return xy print minmax (lessthan,4,7,9,22,2,18) print minmax (grtrthan,4,7,9,22,2,18)

4. Transfer mixing parameters

> def f2 (print pargs: 12) > > f2 (1, 2, 3, 4, 5, 5, 11, 5) 1 (2, 3, 4, 4) {'Yee: 12,' x, 4: 11}

5. Exercise-string ∪ and ∩ operations

> > def intersct (* args): res= [] for x in args [0]: for other in args [1:]: if x not in other:break else:res.append (x) return res > def union (* args): res= [] for seq in args: for x in seq: if not x in res: res.append (x) return res > s1Magne s2if not x in res s3 = "SPAM", "SCAM", "SLAM" > > intersct (s1PhoneS2) ['showing,' Aging,'M'] > > union (S1) S2) ['Sparks,' Packers, 'Aids,' Mises,'C']

6. Class member variables and instance variables

> class C: X = 33 def m (self): X = 44 self.X = 55 > obj = C () > print obj.X 33 > obj.m () > > print obj.X 55 > C.X 33

At this point, the study of "parameter transfer of Python function and detailed explanation of variable scope" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Parameters functions variables functions learning multiple dictionaries more help practical next content copies masterpieces characters strings instances members articles methods Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Microsoft vpn macOS Huawei