[Python Tips] Knowing what exists

Being a good programmer isn't just about knowing how to do everything or even being very good at it. One of the most valuable skills of a good programmer is just knowing something exists. Sometimes it is some obscure not often used feature of the language that when the opportunity comes, it can save a lot of time and frustration.

Steem-Python example

A good example of this is when programming with Steem-Python. One frustration is many data points are returned as strings and not as proper floats.

When you get the sbd_balance of an account, it will be returned as 100 SBD and not 100 which can be directly used in calculations. This is easily fixed with strip() or rstrip(), but if you knew about Amount the job is far easier.

from steem.amount import Amount

my_sbd_balance = '10000000 SBD'
total_sbd_value_usd =  Amount(my_vests).amount * CURRENT_SBD_PRICE

This is a lot easier than always remembering to .rstrip(' SBD'), while they are both fairly easy, amount works with SBD, STEEM, and VESTS. It also has some logic to handle precision and other functionality.

If you look up the Amount class, you will find it just does a split(" ") and unpacks the output into two variables. It makes for cleaner and more understandable code. If you don't know this class exists, you might use split(), you might use rstrip, or you might use a totally different method. Someone reading your code or helping you out might do something totally different.

Python reverse()

Another example is reverse(), this is a newer python feature that allows you to reverse an iterable object. This is extremely helpful when needed, but many people still use slice syntax (which doesn't solve all the time this comes up). Not only is reverse() cleaner and more understandable, it can remove a lot of excess code.

How to 10X your development

Just knowing certain features or libraries exist, even if you don't fully understand how to use them is an extremely powerful skill set. It can speed up your coding 10X and get you out of coding ruts.

New releases

Keeping up to date with new releases (for example Python 3.6 offers a lot of new powerful functionality) can help expose you to many new features that were developed to solve pain points.

My Python Tips Series

H2
H3
H4
3 columns
2 columns
1 column
9 Comments
Ecency