Page 11 of 16

Re: Python tricks

Posted: July 31st, 2019, 11:56 am
by ISayMoo
Sure, you can break the convention. But that's not the Python Way.

Re: Python tricks

Posted: July 31st, 2019, 12:48 pm
by Cuchulainn
Sure, you can break the convention. But that's not the Python Way.
Like zebra crossing in Italy, Python underscores are artistic embellishments?

Newbies buy some gear.

Image

Re: Python tricks

Posted: July 31st, 2019, 1:32 pm
by FaridMoussaoui
There is some "best practice" usage in python.

Python's convention to make an instance variable "private" is to add a prefix "double underscore" __ (a _ for protected).

Python performs name mangling of private variables. Every member with double underscore will be changed to _object._class__variable.
If so required, it can still be accessed from outside the class, but the practice should be refrained.

Re: Python tricks

Posted: July 31st, 2019, 1:35 pm
by katastrofa
Sure, you can break the convention. But that's not the Python Way.
Tim Toady Bicarbonate says:
Image

Re: Python tricks

Posted: July 31st, 2019, 2:38 pm
by Cuchulainn
There is some "best practice" usage in python.

Python's convention to make an instance variable "private" is to add a prefix "double underscore" __ (a _ for protected).

Python performs name mangling of private variables. Every member with double underscore will be changed to _object._class__variable.
If so required, it can still be accessed from outside the class, but the practice should be refrained.
double underscore is fine by me.
In fairness, a CAD library in Python is not advisable. Each to his own. 

However, Python is a great medium through which to learn complex stuff.

Protected variables are bad and harmful!

Re: Python tricks

Posted: July 31st, 2019, 2:39 pm
by Cuchulainn
Sure, you can break the convention. But that's not the Python Way.
Tim Toady Bicarbonate says:
Image
Each sect has its own high priests. C++ is for consenting adults.

Re: Python tricks

Posted: July 31st, 2019, 3:17 pm
by ISayMoo
Sure, you can break the convention. But that's not the Python Way.
Tim Toady Bicarbonate says:
Image
Indeed, people who used Perl should unlearn certain things in order to be happy coding in Python.

Re: Python tricks

Posted: July 31st, 2019, 3:30 pm
by katastrofa
It's like when we discussed Christianity and you told me that I confused it with paganism, but perhaps the other way around. I mean I was baptised, but someone set the church on fire and the certificate perished. (Cuchulainn started about sects.)

Re: Python tricks

Posted: August 1st, 2019, 9:43 pm
by ISayMoo
https://www.python.org/dev/peps/pep-0020/

"There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch."

Re: Python tricks

Posted: August 1st, 2019, 11:19 pm
by katastrofa
should be != is
("Zen of Python"? I think they meant "Tao of Python"...)

Re: Python tricks

Posted: August 2nd, 2019, 1:51 pm
by Cuchulainn
https://www.python.org/dev/peps/pep-0020/

"There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch."
We don't have any choice in the matter.

Image

Re: Python tricks

Posted: August 12th, 2019, 12:56 pm
by Cuchulainn
On a somewhat related comparison, I ported C++ code to price financial derivatives (options) to Python to get a feeling for relative run-time performance., This is essentially a one-step algorithm in a double nested 'for' loop and random number generators are used in both cases (Mersenne Twister 19937)
Conclusion C++ is 60 times faster! 
Just looking at the random number generator part, using Numba improves performance appreciably (Python is 1 1/2 times slower). In more complicated code it is not obvious how to use numba. In this case Python would not be suitable for production purposes but it would be useful for prototyping. 

Re: Python tricks

Posted: August 13th, 2019, 2:58 pm
by Cuchulainn
Probably an oxymoron, but how do you do loop optmisation in Python? Can a JIT compiler choke on it?

https://en.wikipedia.org/wiki/Loop_optimization

Re: Python tricks

Posted: August 15th, 2019, 5:58 pm
by ISayMoo
One optimises a Python loop by removing the loop.
sum_x = 0
for x in xs:
  sum_x += x
to
sum_x = sum(xs)

Re: Python tricks

Posted: August 15th, 2019, 5:58 pm
by ISayMoo