Thursday, April 18, 2019

Python's dynamic nature: sticking an attribute onto an object

- 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,, 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.

# 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:
# Blog:
# Training:
# Product store:
# Twitter:

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):

# 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):

# 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
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.


- 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 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:

Thursday, April 11, 2019

The Vindhyas as seen from Bhimbetka

I was browsing some Wikipedia sites about Indian rivers and mountain ranges, and via a chain of links, came across this page, about the Vindhyas as seen from Bhimbetka.

The Vindhyas are a large mountain range in central India.

I lived in Madhya Pradesh for some years as a teenager. Madhya Pradesh is called M.P. for short; the name means, loosely, middle state, referring to its geographic position in India.

The Vindhyas are partly in Madhya Pradesh, and Bhimbetka is in M.P. too.

I had visited Bhimbetka, and also traveled to, or through, many other places in M.P. generally, and the Vindhyas in particular (both within and outside of M.P., such as in Maharashtra, a neighboring state, for example) as a kid and teenager. I loved many of those places.

Tuesday, April 9, 2019

The Blogger issue (unable to use its UI) seems solved now

By Vasudev Ram

Hi, readers,

The Google Blogger issue that I referred to, here and here, seems to be solved now.

I suspected that it was either an issue at Google's end (e.g. an introduced bug that had not yet been fixed) or some configuration / cookie / cache issue at my end.

I did some stuff to debug it, like logging out and back in, logging in to a different Gmail account and then logging back into the previous one, clearing my browser cache, etc. One or other of those things seems to have worked.

I am writing this current post via the regular Blogger UI, not via the email-to-blog interface that I used in the interim. So if the post appears okay, it likely means that the issue has been resolved. Will write a small Python test post or two in the next few days to verify this, and then resume normal blogging.

Thanks for your patience.

- Vasudev

- Vasudev Ram - Online Python training and consulting

Hit the ground running with my vi quickstart tutorial, vetted by two Windows system administrator friends.

Jump to posts: Python * DLang * xtopdf

Interested in a Python, SQL or Linux course?

Get WP Engine, powerful managed WordPress hosting.

Subscribe to my blog ( by email

My ActiveState Code recipes

Follow me on:

* Gumroad  * LinkedIn

  * Twitter

Do you create online products? Get Convertkit:

Email marketing for digital product creators

Monday, April 8, 2019

Sunday, April 7, 2019

Updated my Codementor profile with my training page link (Python, Linux, SQL, ...)

Hi, readers,

I updated my Codementor profile with my training page link (Python,
Linux, SQL, ...)


And the direct link to the training page is here, with course outlines
and testimonials:

Also, I will be posting in the way I have done for this current post -
i.e. via Blogger's email-to-blog service - which has limited
functionality (e.g. less formatting options) - until this Google
Blogger UI issue (which started for me a few days ago) is resolved:

Until then, sorry for the inconvenience.

And/or may start another blog on a different blogging platform. Will
keep my readers posted about what I decide, and will share the link to
the new blog here if I do create a new one.


Training: Python, SQL, Linux, more:
Web site: