Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Parameter transfer of Python function and detailed explanation of variable scope

2025-03-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report