1566802531
Are you confused by fancy programming terms like functions, classes, methods, libraries, and modules? Do you struggle with the scope of variables? Whether you're a self-taught programmer or a formally trained code monkey, the modularity of code can be confusing. But classes and libraries encourage modular code, and modular code can mean building up a collection of multipurpose code blocks that you can use across many projects to reduce your coding workload. In other words, if you follow along with this article's study of Python functions, you'll find ways to work smarter, and working smarter means working less.
This article assumes enough Python familiarity to write and run a simple script. If you haven't used Python, read my intro to Python article first.
Functions are an important step toward modularity because they are formalized methods of repetition. If there is a task that needs to be done again and again in your program, you can group the code into a function and call the function as often as you need it. This way, you only have to write the code once, but you can use it as often as you like.
Here is an example of a simple function:
#!/usr/bin/env python3import time
def Timer():
print("Time is " + str(time.time() ) )
Create a folder called mymodularity and save the function code as timestamp.py.
In addition to this function, create a file called init.py in the mymodularitydirectory. You can do this in a file manager or a Bash shell:
$ touch mymodularity/init.py
You have now created your own Python library (a “module,” in Python lingo) in your Python package called mymodularity. It’s not a very useful module, because all it does is import the time module and print a timestamp, but it’s a start.
To use your function, treat it just like any other Python module. Here’s a small application that tests the accuracy of Python’s sleep() function, using your mymodularity package for support. Save this file as sleeptest.py outside the mymodularity directory (if you put this into mymodularity, then it becomes a module in your package, and you don’t want that).
#!/usr/bin/env python3import time
from mymodularity import timestampprint(“Testing Python sleep()…”)
modularity
timestamp.Timer()
time.sleep(3)
timestamp.Timer()
In this simple script, you are calling your timestamp module from your mymodularitypackage (twice). When you import a module from a package, the usual syntax is to import the module you want from the package and then use the module name + a dot + the name of the function you want to call (e.g., timestamp.Timer()).
You’re calling your Timer() function twice, so if your timestamp module were more complicated than this simple example, you’d be saving yourself quite a lot of repeated code.
Save the file and run it:
$ python3 ./sleeptest.py
Testing Python sleep()…
Time is 1560711266.1526039
Time is 1560711269.1557732
According to your test, the sleep function in Python is pretty accurate: after three seconds of sleep, the timestamp was successfully and correctly incremented by three, with a little variance in microseconds.The structure of a Python library might seem confusing, but it’s not magic. Python is programmed to treat a folder full of Python code accompanied by an init.py file as a package, and it’s programmed to look for available modules in its current directory first. This is why the statement from mymodularity import timestamp works: Python looks in the current directory for a folder called mymodularity, then looks for a timestamp file ending in .py.
What you have done in this example is functionally the same as this less modular version:
#!/usr/bin/env python3import time
from mymodularity import timestampprint(“Testing Python sleep()…”)
no modularity
print("Time is " + str(time.time() ) )
time.sleep(3)
print("Time is " + str(time.time() ) )
For a simple example like this, there’s not really a reason you wouldn’t write your sleep test that way, but the best part about writing your own module is that your code is generic so you can reuse it for other projects.
You can make the code more generic by passing information into the function when you call it. For instance, suppose you want to use your module to test not the computer’s sleep function, but a user’s sleep function. Change your timestamp code so it accepts an incoming variable called msg, which will be a string of text controlling how the timestamp is presented each time it is called:
#!/usr/bin/env python3import time
updated code
def Timer(msg):
print(str(msg) + str(time.time() ) )
Now your function is more abstract than before. It still prints a timestamp, but what it prints for the user is undefined. That means you need to define it when calling the function.
The msg parameter your Timer function accepts is arbitrarily named. You could call the parameter m or message or text or anything that makes sense to you. The important thing is that when the timestamp.Timer function is called, it accepts some text as its input, places whatever it receives into a variable, and uses the variable to accomplish its task.
Here’s a new application to test the user’s ability to sense the passage of time correctly:
#!/usr/bin/env python3from mymodularity import timestamp
print(“Press the RETURN key. Count to 3, and press RETURN again.”)
input()
timestamp.Timer("Started timer at ")print(“Count to 3…”)
input()
timestamp.Timer("You slept until ")
Save your new application as response.py and run it:
$ python3 ./response.py
Press the RETURN key. Count to 3, and press RETURN again.Started timer at 1560714482.3772075
Count to 3…You slept until 1560714484.1628013
The new version of your timestamp module now requires a msg parameter. That’s significant because your first application is broken because it doesn’t pass a string to the timestamp.Timer function:
$ python3 ./sleeptest.py
Testing Python sleep()…
Traceback (most recent call last):
File “./sleeptest.py”, line 8, in <module>
timestamp.Timer()
TypeError: Timer() missing 1 required positional argument: ‘msg’
Can you fix your sleeptest.py application so it runs correctly with the updated version of your module?
By design, functions limit the scope of variables. In other words, if a variable is created within a function, that variable is available to only that function. If you try to use a variable that appears in a function outside the function, an error occurs.
Here’s a modification of the response.py application, with an attempt to print the msgvariable from the timestamp.Timer() function:
#!/usr/bin/env python3from mymodularity import timestamp
print(“Press the RETURN key. Count to 3, and press RETURN again.”)
input()
timestamp.Timer("Started timer at ")print(“Count to 3…”)
input()
timestamp.Timer("You slept for ")print(msg)
Try running it to see the error:
$ python3 ./response.py
Press the RETURN key. Count to 3, and press RETURN again.Started timer at 1560719527.7862902
Count to 3…You slept for 1560719528.135406
Traceback (most recent call last):
File “./response.py”, line 15, in <module>
print(msg)
NameError: name ‘msg’ is not defined
The application returns a NameError message because msg is not defined. This might seem confusing because you wrote code that defined msg, but you have greater insight into your code than Python does. Code that calls a function, whether the function appears within the same file or if it’s packaged up as a module, doesn’t know what happens inside the function. A function independently performs its calculations and returns what it has been programmed to return. Any variables involved are localonly: they exist only within the function and only as long as it takes the function to accomplish its purpose.
If your application needs information contained only in a function, use a returnstatement to have the function provide meaningful data after it runs.
They say time is money, so modify your timestamp function to allow for an imaginary charging system:
#!/usr/bin/env python3import time
def Timer(msg):
print(str(msg) + str(time.time() ) )
charge = .02
return charge
The timestamp module now charges two cents for each call, but most importantly, it returns the amount charged each time it is called.
Here’s a demonstration of how a return statement can be used:
#!/usr/bin/env python3from mymodularity import timestamp
print(“Press RETURN for the time (costs 2 cents).”)
print(“Press Q RETURN to quit.”)total = 0
while True:
kbd = input()
if kbd.lower() == “q”:
print(“You owe $” + str(total) )
exit()
else:
charge = timestamp.Timer("Time is ")
total = total+charge
In this sample code, the variable charge is assigned as the endpoint for the timestamp.Timer() function, so it receives whatever the function returns. In this case, the function returns a number, so a new variable called total is used to keep track of how many changes have been made. When the application receives the signal to quit, it prints the total charges:
$ python3 ./charge.py
Press RETURN for the time (costs 2 cents).
Press Q RETURN to quit.Time is 1560722430.345412
Time is 1560722430.933996
Time is 1560722434.6027434
Time is 1560722438.612629
Time is 1560722439.3649364
q
You owe $0.1
Functions don’t have to be created in separate files. If you’re just writing a short script specific to one task, it may make more sense to just write your functions in the same file. The only difference is that you don’t have to import your own module, but otherwise the function works the same way. Here’s the latest iteration of the time test application as one file:
#!/usr/bin/env python3import time
total = 0
def Timer(msg):
print(str(msg) + str(time.time() ) )
charge = .02
return chargeprint(“Press RETURN for the time (costs 2 cents).”)
print(“Press Q RETURN to quit.”)while True:
kbd = input()
if kbd.lower() == “q”:
print(“You owe $” + str(total) )
exit()
else:
charge = Timer("Time is ")
total = total+charge
It has no external dependencies (the time module is included in the Python distribution), and produces the same results as the modular version. The advantage is that everything is located in one file, and the disadvantage is that you cannot use the Timer() function in some other script you are writing unless you copy and paste it manually.
A variable created outside a function has nothing limiting its scope, so it is considered a global variable.
An example of a global variable is the total variable in the charge.py example used to track current charges. The running total is created outside any function, so it is bound to the application rather than to a specific function.
A function within the application has access to your global variable, but to get the variable into your imported module, you must send it there the same way you send your msg variable.
Global variables are convenient because they seem to be available whenever and wherever you need them, but it can be difficult to keep track of their scope and to know which ones are still hanging around in system memory long after they’re no longer needed (although Python generally has very good garbage collection).
Global variables are important, though, because not all variables can be local to a function or class. That’s easy now that you know how to send variables to functions and get values back.
You’ve learned a lot about functions, so start putting them into your scripts—if not as separate modules, then as blocks of code you don’t have to write multiple times within one script.
Thanks For Visiting, Keep Visiting. If you liked this post, share it with all of your programming buddies!
Further reading
☞ Building a Blockchain with Python - Full
☞ Python and Django Full Stack Web Developer Bootcamp
☞ Python for Time Series Data Analysis
☞ Python Programming For Beginners From Scratch
☞ Beginner’s guide on Python: Learn python from scratch! (New)
☞ Python for Beginners: Complete Python Programming
☞ Django 2.1 & Python | The Ultimate Web Development Bootcamp
☞ Python eCommerce | Build a Django eCommerce Web Application
☞ Python Django Dev To Deployment
Originally published on opensource.com
#python #web-development #devops
1619510796
Welcome to my Blog, In this article, we will learn python lambda function, Map function, and filter function.
Lambda function in python: Lambda is a one line anonymous function and lambda takes any number of arguments but can only have one expression and python lambda syntax is
Syntax: x = lambda arguments : expression
Now i will show you some python lambda function examples:
#python #anonymous function python #filter function in python #lambda #lambda python 3 #map python #python filter #python filter lambda #python lambda #python lambda examples #python map
1619518440
Welcome to my Blog , In this article, you are going to learn the top 10 python tips and tricks.
…
#python #python hacks tricks #python learning tips #python programming tricks #python tips #python tips and tricks #python tips and tricks advanced #python tips and tricks for beginners #python tips tricks and techniques #python tutorial #tips and tricks in python #tips to learn python #top 30 python tips and tricks for beginners
1602968400
Python is awesome, it’s one of the easiest languages with simple and intuitive syntax but wait, have you ever thought that there might ways to write your python code simpler?
In this tutorial, you’re going to learn a variety of Python tricks that you can use to write your Python code in a more readable and efficient way like a pro.
Swapping value in Python
Instead of creating a temporary variable to hold the value of the one while swapping, you can do this instead
>>> FirstName = "kalebu"
>>> LastName = "Jordan"
>>> FirstName, LastName = LastName, FirstName
>>> print(FirstName, LastName)
('Jordan', 'kalebu')
#python #python-programming #python3 #python-tutorials #learn-python #python-tips #python-skills #python-development
1602666000
Today you’re going to learn how to use Python programming in a way that can ultimately save a lot of space on your drive by removing all the duplicates.
In many situations you may find yourself having duplicates files on your disk and but when it comes to tracking and checking them manually it can tedious.
Heres a solution
Instead of tracking throughout your disk to see if there is a duplicate, you can automate the process using coding, by writing a program to recursively track through the disk and remove all the found duplicates and that’s what this article is about.
But How do we do it?
If we were to read the whole file and then compare it to the rest of the files recursively through the given directory it will take a very long time, then how do we do it?
The answer is hashing, with hashing can generate a given string of letters and numbers which act as the identity of a given file and if we find any other file with the same identity we gonna delete it.
There’s a variety of hashing algorithms out there such as
#python-programming #python-tutorials #learn-python #python-project #python3 #python #python-skills #python-tips
1597751700
Magic Methods are the special methods which gives us the ability to access built in syntactical features such as ‘<’, ‘>’, ‘==’, ‘+’ etc…
You must have worked with such methods without knowing them to be as magic methods. Magic methods can be identified with their names which start with __ and ends with __ like init, call, str etc. These methods are also called Dunder Methods, because of their name starting and ending with Double Underscore (Dunder).
Now there are a number of such special methods, which you might have come across too, in Python. We will just be taking an example of a few of them to understand how they work and how we can use them.
class AnyClass:
def __init__():
print("Init called on its own")
obj = AnyClass()
The first example is _init, _and as the name suggests, it is used for initializing objects. Init method is called on its own, ie. whenever an object is created for the class, the init method is called on its own.
The output of the above code will be given below. Note how we did not call the init method and it got invoked as we created an object for class AnyClass.
Init called on its own
Let’s move to some other example, add gives us the ability to access the built in syntax feature of the character +. Let’s see how,
class AnyClass:
def __init__(self, var):
self.some_var = var
def __add__(self, other_obj):
print("Calling the add method")
return self.some_var + other_obj.some_var
obj1 = AnyClass(5)
obj2 = AnyClass(6)
obj1 + obj2
#python3 #python #python-programming #python-web-development #python-tutorials #python-top-story #python-tips #learn-python