Category: function

How to called Class instance from given string variable in Python

How to called Class instance from given string variable in Python

This is pretty interesting. You have give string list like “InsuranceUser”, “InsuranceUserProfile”, etc. That are string. Now, you should called class that have name same with the given string. I will show in Django models as more real explanation. To fetch InsuranceUser models, usually we do : 123from insurance.models import InsuranceUser user_list = InsuranceUser.objects.all()

How to make pep8 for long attribute / method chain in Python

How to make pep8 for long attribute / method chain in Python

When we use 80 lines rules, sometimes we facing some difficult cases like long attributes. Usually this also happen in Django which need nested method chain for calling models. For instance : 1user_list = User.objects.filter(created__year=2012).order_by(‘user’).values(‘user’).distinct() We should shorten this long attributes. We should know that blackslash is “evil” in python. But, at this cases we …

+ Read More

Deep Dive into Python Path Construction

Deep Dive into Python Path Construction

Today I found great tutorial that explain how sys.path is working. Follow this link to see through : If a script is executed, the interpreter sets the first entry of sys.path to that script’s directory. If Python is launched interactively, the first entry is the empty string (“”), meaning Python will scan the present working …

+ Read More

Understanding about namespaces, attributes and scopes in Python

Understanding about namespaces, attributes and scopes in Python

I assume you don’t understand about namespaces, attributes and scopes in Python. Because I will explain about this three things that mostly missed by people who start learning with Python. Actually, this things will help us in the future about understanding Python even for advanced programmer. So, let begin with namespaces. Namespaces according to Wikipedia …

+ Read More

Understanding arguments *args and **kwargs in Python

Understanding arguments *args and **kwargs in Python

In Python, we usually need passing variable through arguments to functions. There are 2 things that commonly need to know about passing arguments into function which called *args and **kwargs. What this is ? *args This single asterisk form is used to pass non-keyworded, variable-length argument list. For instance : 1234567# Single asterisk form def …

+ Read More