SERVING THE QUANTITATIVE FINANCE COMMUNITY

ISayMoo
Posts: 2250
Joined: September 30th, 2015, 8:30 pm

### Re: Python tricks

Sure, you can break the convention. But that's not the Python Way.

Cuchulainn
Posts: 60838
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

### Re: Python tricks

Sure, you can break the convention. But that's not the Python Way.
Like zebra crossing in Italy, Python underscores are artistic embellishments?

http://www.datasimfinancial.com
http://www.datasim.nl

Approach your problem from the right end and begin with the answers. Then one day, perhaps you will find the final question..
R. van Gulik

FaridMoussaoui
Posts: 474
Joined: June 20th, 2008, 10:05 am
Location: Genève, Genf, Ginevra, Geneva

### Re: Python tricks

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.

katastrofa
Posts: 8639
Joined: August 16th, 2007, 5:36 am
Location: Alpha Centauri

### Re: Python tricks

Sure, you can break the convention. But that's not the Python Way.

Cuchulainn
Posts: 60838
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

### Re: Python tricks

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!
Last edited by Cuchulainn on July 31st, 2019, 2:41 pm, edited 1 time in total.
http://www.datasimfinancial.com
http://www.datasim.nl

Approach your problem from the right end and begin with the answers. Then one day, perhaps you will find the final question..
R. van Gulik

Cuchulainn
Posts: 60838
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

### Re: Python tricks

Sure, you can break the convention. But that's not the Python Way.
Each sect has its own high priests. C++ is for consenting adults.
http://www.datasimfinancial.com
http://www.datasim.nl

Approach your problem from the right end and begin with the answers. Then one day, perhaps you will find the final question..
R. van Gulik

ISayMoo
Posts: 2250
Joined: September 30th, 2015, 8:30 pm

### Re: Python tricks

Sure, you can break the convention. But that's not the Python Way.
Indeed, people who used Perl should unlearn certain things in order to be happy coding in Python.

katastrofa
Posts: 8639
Joined: August 16th, 2007, 5:36 am
Location: Alpha Centauri

### Re: Python tricks

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

ISayMoo
Posts: 2250
Joined: September 30th, 2015, 8:30 pm

### Re: Python tricks

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

katastrofa
Posts: 8639
Joined: August 16th, 2007, 5:36 am
Location: Alpha Centauri

### Re: Python tricks

should be != is
("Zen of Python"? I think they meant "Tao of Python"...)

Cuchulainn
Posts: 60838
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

### Re: Python tricks

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.

http://www.datasimfinancial.com
http://www.datasim.nl

Approach your problem from the right end and begin with the answers. Then one day, perhaps you will find the final question..
R. van Gulik

Cuchulainn
Posts: 60838
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

### Re: Python tricks

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.
http://www.datasimfinancial.com
http://www.datasim.nl

Approach your problem from the right end and begin with the answers. Then one day, perhaps you will find the final question..
R. van Gulik

Cuchulainn
Posts: 60838
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

### Re: Python tricks

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
http://www.datasimfinancial.com
http://www.datasim.nl

Approach your problem from the right end and begin with the answers. Then one day, perhaps you will find the final question..
R. van Gulik

ISayMoo
Posts: 2250
Joined: September 30th, 2015, 8:30 pm

### Re: Python tricks

One optimises a Python loop by removing the loop.
sum_x = 0
for x in xs:
sum_x += x
to
sum_x = sum(xs)

ISayMoo
Posts: 2250
Joined: September 30th, 2015, 8:30 pm