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

How to use Python function

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "how to use Python functions". The explanation in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian and go deep into it slowly to study and learn "how to use Python functions" together!

Please guess how this code works:

import random import time people = ['kingname',' Wang Xiaoyi ', ' Li Xiaoer','Zhang Xiaosan',' Liu Xiaosi ', ' Lu Xiaowu ', ' Ma Xiaoliu ', ' Zhou Xiaoqi','Ding Xiaoba', 'Zhu Xiaojiu'] for i in range(1, 11): lucky_guy = random.choice(people) print(f'th {i} raffle, winning user: {lucky_guy}') time.sleep(1)

Do you think that after this code runs, the result will be as shown in the following figure?

But in fact, I can let the output change as I wish, for example, like this gif below, all output is me:

Instead of looking down, put down your phone, write your own code, and try out how to achieve the effect in gif.

Let's decrypt it for everyone.

To achieve this effect, only two knowledge points are needed:

HarmonyOS Technology Community

Python comes with modules that can be overridden.

Python imports are imported only once at the same runtime

Let's look at the first knowledge point. Python's built-in modules can be overridden, so let's define a function first:

def choice(option): return 'kingname'

Next, override random.choice with this function:

import random random.choice = choice

Now, no matter what parameters are passed to random.choice, it always returns kingname, and the effect is as follows:

At this point, you might say, what about when someone else writes code and imports random again? Random.Choice has not been changed back?

In fact, it doesn't, because Python's package import mechanism dictates that, within each runtime, each package is valid only the first time it is imported, so all subsequent import random are invalid as long as it is still in the current runtime.

So, even if you re-import the random module, random.choice is still the code you modified. So when you execute it again, you will find that the returned data is still the data you want, as shown in the following figure:

Some people may say that this is easy to see through ah, others just write some test data, run random.choice([123, 456]), found that the return is kingname, this is not exposed?

In fact, there is no need to worry, we can do this:

If the alternative list does not contain kingname, use the native random.choice

If the alternative list contains kingname, kingname is returned with a 60% probability.

To do this, we can write code like this:

First restart the current Jupyter kernel, restore random to default, and then code:

import random origin_choice = random.choice def choice(option): if 'kingname' not in option or random.randint(1, 10) > 6: return origin_choice(option) return 'kingname' random.choice = choice

After this substitution, when kingname is in the alternative list, kingname has a 60% probability of being selected, as shown in the following figure:

When kingname is not in the alternative list, everything is fine, as shown in the following figure:

Thank you for reading, the above is the content of "how to use Python function", after the study of this article, I believe everyone has a deeper understanding of how to use Python function, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

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

Development

Wechat

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

12
Report