# Eva was lured by a PYTHON and we, her descendants, have to bear the divine punishment... Python 39 Joe viết ngày 21/09/2017

Hi

PYTHON is the hype in the democratization of IT-world. Newbies usually hate everything that requires them to learn too much, especially about the boring details like byte, bit, double, float...Life isn't a free lunch. But PYTHON gives them a free lunch. Example

C:\Python>python
Python 3.6.2 (v3.6.2:5fd33b5, Jul  8 2017, 04:57:36) [MSC v.1900 64 bit (AMD64)] on win32
>>> a = 4
>>> a
4
>>> a = 4.5
>>> a
4.5
>>>


It as clear as the sun's shining: a = 4, 4 is an integer. Or a = 4.5, 4.5 must be a float (or a double...who cares?)

Why on earth that C/C++/C# or JAVA is so despotic and demands the poor newbies to declare explicitly int or float? Like Eva, all easy-going newbies start to bite the apple offered by PYTHON. They, not their unborn descendants, have to bear the divine penalty and suffer the consequences.

What penalty? Well, the penalty is subtle and usually painless. Like toothache. It comes suddenly and terrorizes you horribly, then it disappears abruptly. Normally PYTHON is very forgiving. For example, you "develop" an app in PYTHON like this

>>> b = 5
>>> a = 3
>>> c = a/b
>>> c
0.6
>>>


Splendid, isn't it? a and b are both integer, the result c is a float (or a double, who cares?) It's a high-level OOPL, so PYTHON simply knows that a and b are both int and c a float, right? But when a PYTHON script, eh sorry, app runs into troubles PYTHON is not very friendly and verbose. Sometimes its words are so cryptic that you have to google and try to decipher its words with some uneasiness. Example

>>> print "Joe"
File "<stdin>", line 1
print "Joe"
^
SyntaxError: Missing parentheses in call to 'print'
>>>


You scratch your head and wonder what the heck is the File "< stdin >", and line 1. Where is the line 1?

Because you're smart you start to study the error message: SyntaxError: Missing parentheses in call to 'print'. You scratch your head again...and think...

Yesterday I wrote so and it worked fine. Today I've upgraded my PYTHON and that damn shit. Why?

You start to study the release notes, eh sorry, the changelog and get lost. Why? 126 pages (PYTHON 3.6.2). A thick book full of cryptic announcements...like this one here:

bpo-31315: Fix an assertion failure in imp.create_dynamic(), when spec.name is not a string. Patch by Oren Milman.

after wasting time on several pages you lose your nerve and give up, then you ask around and hope to get an answer. Yes. You usually get one in any public forum where someone who could give you an answer because (s)he's badly suffered to find out the solution.

Contrary to the rest of the world that Software upgrade is always upwards compatible, rarely backwards incompatible PYTHON is a real individual snake that is immediately independent after hatching out of the egg. Also, snake PYTHON 3 has "nothing" to do with the "old snake" PYTHON 2.

However, it's harmless with the trivial SyntaxErrors. It's more complicate and very frustrating when the new upgrade misses some "modules" that existed in an old one, but incompatible with the new upgrade.

Now what? You have no other choice than to pip the modules down to your computer.

pip install blabla


Or in other words: you have to beam the new things down to earth. And you may get a very nice flattery like this

Note
The use of Python 3 is highly preferred over Python 2. Consider upgrading your applications and infrastructure if you find yourself still using Python 2 in production today. If you are using Python 3, congratulations — you are indeed a person of excellent taste. —Kenneth Reitz

It isn't very nice at all. It's the Carrot-and-Stick policy. The stick: "consider upgradding...", the carrot: "you are indeed a person of excellent taste."

Gosh! What a cheesiness!

Part II,
Eva, PYTHON and her newbie-descendants

Kenneth Reitz said in the 126-pages "introduction Changelog"

The use of Python 3 is highly preferred over Python 2. Consider upgrading your applications and infrastructure if you find yourself still using Python 2 in production today. If you are using Python 3, congratulations — you are indeed a person of excellent taste.

And I sensed that his statement was very cheesy... Cheesy? Yes, it's quite cheesy to flatter people after letting them suffer from incompatible upgrade. Well, let's forgive the guy and enjoy the newly-hatched PYTHON 3.6.2.

Snake is by nature very flexible and quite adaptive. Therefore PYTHON is not an exception. It could survive for a month or more without eating anything. But when it gets a big prey it swallows the whole prey down -bones, hair, everything. Nothing gets lost. Exactly like its natural brethren IT-PYTHON swallows down everything. Eva and her newbie-descendants love that because Swallowing-down-everything means "Rapid Application Development" or RAD for short.

A tool that lets them quickly improvise the thing they need without having to "study" or "learn" the details. The main thing is that it works. And that is the driving force why academic eggheads from other disciplines than Computer Science love PYTHON. They could improvisingly write some "apps" to test or to demonstrate their work. And the foot folks, thanks to democratization, have access to this wonderful RAD. Are they all becoming eggheads ? I don't know...

Due to its improvising characters and PYTHON's forgivingness (i.e. adaptiveness and flexibleness) "dangerous" traps emerge from unawareness and carelessness. And that could cause the biggest headaches.

Let me show you some hurtful pitfalls when you work with PYTHON.

Pitfall No.1: PYTHON never bite. It expects that its prey (i.e. developer) comes to it and does what it expects so that it could conveniently "constrict" its prey. Or in PYTHON terminology: Indentation. No { } like in C/C++/C#/JAVA. Very simple, but easy to fall into this primitive pitfall.

C:\Python>python
Python 3.6.2 (v3.6.2:5fd33b5, Jul  8 2017, 04:57:36) [MSC v.1900 64 bit (AMD64)] on win32
>>> def cow(moo=[]):
... moo.append("moooooo")
File "<stdin>", line 2
moo.append("moooooo")
^
IndentationError: expected an indented block
>>>


Of course, you should know what "indentation" means. Right?

Pitfall No.2: Suppose that you didn't fall into Pitfall No.1 and you're able to create this little app called cow.py

def cow(moo=[]) :   # optinal default for function cow
moo.append("Moooooooo!")
return moo
# first invocation
cow()
# second invocation
cow()


You proudly run it...

C:\Python>python cow.py

C:\Python>


NOTHING happens...you see nothing. You scratch your head and try to "code" directly with the wild PYTHON

C:\Python>python
Python 3.6.2 (v3.6.2:5fd33b5, Jul  8 2017, 04:57:36) [MSC v.1900 64 bit (AMD64)] on win32
>>>>>> def cow(moo=[]) :   # optinal default for function cow
...     moo.append("Moooooooo!")
...     return moo
... # first invocation
... cow()
File "<stdin>", line 5
cow()
^
SyntaxError: invalid syntax
>>> # second invocation
... cow()
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
NameError: name 'cow' is not defined
>>>


What the heck is wrong?...As I said in my previous "blog" PYTHON is a dear skinflint. Its "ErrorMessage" is neither friendly, nor precise, but very taciturn. The pitfall is the missing "empty" line between "return moo" and the comment "# first invocation". It's the so-called "end of block" (or the } in other OOPL). Finally you got it.

Microsoft Windows [Version 10.0.15063]
(c) 2017 Microsoft Corporation. Alle Rechte vorbehalten.

C:\Python>python
Python 3.6.2 (v3.6.2:5fd33b5, Jul  8 2017, 04:57:36) [MSC v.1900 64 bit (AMD64)] on win32
>>> def cow(moo=[]) :
...     moo.append("Moooooo!")
...     return moo
...
>>> cow()
['Moooooo!']
>>> cow()
['Moooooo!', 'Moooooo!']
>>>


After every cow() invocation you get a Moooooo! more...A bit weird, isn't it? Well, you set the weirdness aside and correct your app cow.py

Pitfall No.3: PYTHON in the wildness behaves differently from your domesticated PYTHON. The interpreter (or direct) PYTHON is the wild one, the "scripted" PYTHON is the domesticated one. Hence:

C:\Python>python cow.py

C:\Python>


You get NO "Moooooo!". Why? Because wildness is different from domestics. That's the reason. To get the same weirdness of wild PYTHON you have to teach your domesticated PYTHON differently. Namely like this

def cow(moo=[]) :   # optinal default for function cow
moo.append("Moooooooo!")
return moo
# first invocation
print (cow())
# second invocation
print (cow())


The print makes your domesticated PYTHON behaves wildly like its brethren in the wildness. And you get what you want.

C:\Python>python cow.py
['Moooooooo!']
['Moooooooo!', 'Moooooooo!']

C:\Python>


The reason that cow.py spit out every time "Moooooo!" is that PYTHON always assumes that you let it work for you. Also, it provides a default for you when you opt an optional argument for your function. Also it adds each time the "Moooooo!" into the array moo[] when the method (or function) cow() is invoked. And that is the last pitfall I want to show you. If I have more time I'll continue this kipalog. To avoid the "repeated-unnecessarily" of "Moooooo!" you have to recode the beast PYTHON as following:

def cow(moo = None) :   # None is used instead of moo[]
if moo is None:     # or if not bar:
moo = []        # define moo
moo.append("Moooooooo!")
return moo

# first invocation
print (cow())
# second invocation
print (cow())


Now the beast behaves perfectly as we want to have...

C:\Python>python cow.py
['Moooooooo!']
['Moooooooo!']

C:\Python>


Joe

Bình luận

Bỏ hay Hay
{{comment.like_count}}
{{ comment_error }}

Hiển thị thử

Chỉnh sửa

### Joe

8 bài viết.
35 người follow
Cùng một tác giả
22 9
Fuzzy Logic and Machine Learning Hi First of all: I apologize everyone for my writing in English. I come to this site because someone of Daynhauh...
Joe viết 6 tháng trước
22 9
9 3
Hi Giaosucan wrote an interesting article and a good technical overview about Bitcoin (Blockchain). If anyone does have interest on the Blockchain...
Joe viết 6 tháng trước
9 3
4 0
Hi Java developers certainly know the famous dogma Single Inheritance. It sets the rule that every JAVA object is always derived from JAVA class O...
Joe viết 6 tháng trước
4 0
Bài viết liên quan
1 0
Mở đầu Như đã nói ở bài trước, mình đang nghiên cứu về Spark nên cần log lại một số thứ để dành sau này dùng đến :smile: Đối tượng hướng đến vẫn ...
Phạm Quốc Thắng viết gần 2 năm trước
1 0
4 3
Observer pattern (python example) 1. Observer là gì : Theo như (Link) Observer Pattern là : A software design pattern in which an object, calle...
Khôi Trọng Nguyễn viết hơn 1 năm trước
4 3
0 0
Web Framework Flask định nghĩa route bằng annotations kiểu như @route('/users/add', methods='GET']) def user_add(): pass Lợi thế của cách là...
studybot viết hơn 2 năm trước
0 0

kipalog

bình luận

8 bài viết.
35 người follow

Đầu mục bài viết

Vẫn còn nữa! x

Kipalog vẫn còn rất nhiều bài viết hay và chủ đề thú vị chờ bạn khám phá!