-
By Vasudev Ram - Online Python training / SQL training / Linux training
Hi, readers,
[This is a
beginner-level Python post.]
Python, being a dynamic language, has some interesting features that some static languages may not have (and vice versa too, of course).
One such feature, which I noticed a while ago, is that you can add an attribute to a Python object even after it has been created. (Conditions apply.)
I had used this feature some time ago to work around some implementation issue in a rudimentary RESTful server that I created as a small teaching project. It was based on the
BaseHTTPServer module.
Here is a (different) simple example program,
stick_attrs_onto_obj.py, that demonstrates this Python feature.
My informal term for this feature is "sticking an attribute onto an object" after the object is created.
Since the program is simple, and there are enough comments in the code, I will not explain it in detail.
# stick_attrs_onto_obj.py
# A program to show:
# 1) that you can "stick" attributes onto a Python object after it is created, and
# 2) one use of this technique, to count the number# of calls to a function.
# Copyright 2019 Vasudev Ram
# Web site: https://vasudevram.github.io
# Blog: https://jugad2.blogspot.com
# Training: https://jugad2.blogspot.com/p/training.html
# Product store: https://gumroad.com/vasudevram
# Twitter: https://twitter.com/vasudevram
from __future__ import print_function
# Define a function.
def foo(arg):
# Print something to show that the function has been called.
print("in foo: arg = {}".format(arg))
# Increment the "stuck-on" int attribute inside the function.
foo.call_count += 1
# A function is also an object in Python.
# So we can add attributes to it, including after it is defined.
# I call this "sticking" an attribute onto the function object.
# The statement below defines the attribute with an initial value,
# which is changeable later, as we will see.
foo.call_count = 0
# Print its initial value before any calls to the function.
print("foo.call_count = {}".format(foo.call_count))
# Call the function a few times.
for i in range(5):
foo(i)
# Print the attribute's value after those calls.
print("foo.call_count = {}".format(foo.call_count))
# Call the function a few more times.
for i in range(3):
foo(i)
# Print the attribute's value after those additional calls.
print("foo.call_count = {}".format(foo.call_count))
And here is the output of the program:
$ python stick_attrs_onto_obj.py
foo.call_count = 0
in foo: arg = 0
in foo: arg = 1
in foo: arg = 2
in foo: arg = 3
in foo: arg = 4
foo.call_count = 5
in foo: arg = 0
in foo: arg = 1
in foo: arg = 2
foo.call_count = 8
There may be other ways to get the call count of a function, including using a profiler, and maybe by using a closure or decorator or other way. But this way is really simple. And as you can see from the code, it is also possible to use it to find the number of calls to the function, between any two points in the program code. For that, we just have to store the call count in a variable at the first point, and subtract that value from the call count at the second point. In the above program, that would be
8 - 5 = 3, which matches the
3 that is the number of calls to function
foo made by the 2nd for loop.
Enjoy.
-
Vasudev Ram - Online Python training and consulting
I conduct online courses on
Python programming,
Unix / Linux commands and shell scripting and
SQL programming and
database design, with
course material and
personal coaching sessions.
The course details and testimonials are here.
Contact me for details of course content, terms and schedule.
Try
FreshBooks: Create and send professional looking invoices in less than 30 seconds.
Getting a new web site or blog, and want to help preserve the environment at the same time? Check out
GreenGeeks.com web hosting.
Sell your digital products via
DPD: Digital Publishing for Ebooks and Downloads.
Learning Linux? Hit the ground running with my
vi quickstart tutorial. I wrote it at the request of two Windows system administrator friends who were given additional charge of some Unix systems. They later told me that it helped them to quickly start using vi to edit text files on Unix. Of course, vi/vim is one of the
most ubiquitous text editors around, and works on most other common operating systems and on some uncommon ones too, so the knowledge of how to use it will carry over to those systems too.
Check out
WP Engine, powerful WordPress hosting.
Creating online products for sale?
Check out ConvertKit, email marketing for online creators.
Teachable: feature-packed course creation platform, with unlimited video, courses and students.
Posts about:
Python *
DLang *
xtopdf
My ActiveState Code recipes
Follow me on: