每天进步一点点:原来Python不支持函数重载
cn·@oflyhigh·
0.000 HBD每天进步一点点:原来Python不支持函数重载
今天在编写Python脚本时遇到一个问题,我写一个了函数实现了一些功能,假设这个函数为func_A()。  (图源 :[pixabay](https://pixabay.com/)) >`def func_A(var_1, var_2, var_3,var_4):` 但是其实`var_1、var_2, var3,,var_4`是一组高度相关的数据,所以我打算把它们放到一个字典(dictionary)中vars,然后实现一个函数直接处理这个字典。 我首先想到的就是重载 `func_A`,定义如下: >`def func_A(vars):` 然后实现中调用原本的之前的函数: >`return func_A(vars["var1"], vars["var2"], vars["var3"], vars["var4"])` 结果调用时却出现了如下提示, > 好吧,尽管我的思路很是***清奇***,但是我感觉我没做错什么呀?为啥会出错呢? 找了一些文章,原来***Python并不支持函数重载***,当然也并不是没有办法实现,比如说可以用默认参数,传入不定数量参数,或者使用什么`@overload`修饰符啥的。 # 解决 但是对我而言,没必要那么折腾,最简单的方式是给函数改个名字,比如说第二个函数就叫`func_B`好了。 或者既然我觉得`var_1、var_2, var3,,var_4`是一组高度相关的数据,不应该被分散调用,那么干脆就把`func_A(var_1, var_2, var_3,var_4)`这个函数删除,直接用`func_A(vars)`实现好了。 # 结论 Python 并不支持重载,所以我使用了改函数名或者重新实现函数来解决。 我一贯的原则是,不要纠结语言啊语法等问题,把问题解决了就好,哈哈。 遇到问题,没必要和问题死磕,绕过去就好了嘛。不过知道了Python不支持重载,那么以后就不会犯这样的错误啦。 # 参考链接 * [Overloaded functions in python?](https://stackoverflow.com/questions/7113032/overloaded-functions-in-python) * [Method overloading](https://pythonspot.com/method-overloading/) * [python 中为什么不需要重载](https://www.cnblogs.com/erbaodabao0611/p/7490439.html) ---- <center><strong>Vote For Me As Witness</strong> https://steemit.com/~witnesses type in **`oflyhigh`** and click ***`VOTE`*** [](https://steemit.com/~witnesses) [Vote @oflyhigh via Steemconnect](https://steemconnect.com/sign/account-witness-vote?witness=oflyhigh&approve=1) <strong>Thank you!</strong></center>
👍 liangfengyouren, bambugrove, astros, money-way, blues-wclouds, cryptouno, eval, julian2013, archisteem, evelaurie, yalehazlitt, eltonwright, zorapolly, yettawild, bobarchibald, annasandy, sskiller, steeming-hot, aximkrat, vinothjesus, cryptopie, kimzwarch, holmesian, pataty69, munhenhos, bhaski, rafaelmonteiro, auntigormint, alexis555, ms8988, ikonik, exec, chinadaily, newhope, lemminon, janiceting, steemwhalepower, yulan, wongshiying, thecryptodrive, yyyy, davidke20, devilwsy, elizacheng, aellly, bichen, a-secondchance, midnightoil, maiyude, somebody, htliao, hmayak, xiaokongcom, moneybaby, laoyao, lingfei, ethansteem, xiaohui, deanliu, oflyhigh, lalala, helene, blackbunny, andrewma, steemprotect, ricko66, rea, busy.pay, jamzmie, accelerator, casberp, map10k, astromaniac, technologynepal, wylo, hamsa.quality, kymio, phatima, bukiland, yestermorrow, hyborian-strain, canbethisone, aabb, cryptohustler, huatanzhang, windowglass, rivalhw, cn-reader, bboyady, winniex, sanzo, khalilad, fastiduos, silentlucidity53, abetterworld, sweethoney, chenlocus, rebecca80, weavingwords, chann, alijewel, mrliga, freedom-fighter, stakuza, heyeshuang, razor80, coindzs, khayziljoy, raku, bearpaw, chaerin, ethanlee, cryptoknight27, lkvictor2005, speeding, laissez-faire, raili, wenjiewu, kingofdew, oldman28, hongman96, austinsandersco, ebejammin, blockchainbilly, haiyangdeperci, jkkim, jiangchen, dgorbunov, jianghao, teachblogger, fredo77200, agoha, walkinharmony, frankintaiwan, lancy, that1consultant, nitro.live, asterix87, historylover, resteeming, davaowhenyo, btccurrency1, fanso, allenshayzar, ravenousappetite, vfxness, ooairbb, superman01, xiaoshancun, partiko, goodcontentbot, cnbuddy, tumutanzi, minloulou, victory622, jincheng, ace108, sweetsssj, alex2016, andreavu, justyy, moneybabe, kellyer, adm, jongreat, bxt, idx, abit,