SERVING THE QUANTITATIVE FINANCE COMMUNITY

 
User avatar
ISayMoo
Posts: 2213
Joined: September 30th, 2015, 8:30 pm

Re: Python tricks

July 31st, 2019, 11:56 am

Sure, you can break the convention. But that's not the Python Way.
 
User avatar
Cuchulainn
Posts: 60522
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

Re: Python tricks

July 31st, 2019, 12:48 pm

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
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
 
User avatar
FaridMoussaoui
Posts: 457
Joined: June 20th, 2008, 10:05 am
Location: Genève, Genf, Ginevra, Geneva

Re: Python tricks

July 31st, 2019, 1:32 pm

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.
 
User avatar
katastrofa
Posts: 8530
Joined: August 16th, 2007, 5:36 am
Location: Alpha Centauri

Re: Python tricks

July 31st, 2019, 1:35 pm

Sure, you can break the convention. But that's not the Python Way.
Tim Toady Bicarbonate says:
Image
 
User avatar
Cuchulainn
Posts: 60522
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

Re: Python tricks

July 31st, 2019, 2:38 pm

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
 
User avatar
Cuchulainn
Posts: 60522
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

Re: Python tricks

July 31st, 2019, 2:39 pm

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.
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
 
User avatar
ISayMoo
Posts: 2213
Joined: September 30th, 2015, 8:30 pm

Re: Python tricks

July 31st, 2019, 3:17 pm

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.
 
User avatar
katastrofa
Posts: 8530
Joined: August 16th, 2007, 5:36 am
Location: Alpha Centauri

Re: Python tricks

July 31st, 2019, 3:30 pm

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.)
 
User avatar
ISayMoo
Posts: 2213
Joined: September 30th, 2015, 8:30 pm

Re: Python tricks

August 1st, 2019, 9:43 pm

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."
 
User avatar
katastrofa
Posts: 8530
Joined: August 16th, 2007, 5:36 am
Location: Alpha Centauri

Re: Python tricks

August 1st, 2019, 11:19 pm

should be != is
("Zen of Python"? I think they meant "Tao of Python"...)
 
User avatar
Cuchulainn
Posts: 60522
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

Re: Python tricks

August 2nd, 2019, 1:51 pm

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
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
 
User avatar
Cuchulainn
Posts: 60522
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

Re: Python tricks

August 12th, 2019, 12:56 pm

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
 
User avatar
Cuchulainn
Posts: 60522
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

Re: Python tricks

August 13th, 2019, 2:58 pm

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
 
User avatar
ISayMoo
Posts: 2213
Joined: September 30th, 2015, 8:30 pm

Re: Python tricks

August 15th, 2019, 5:58 pm

One optimises a Python loop by removing the loop.
sum_x = 0
for x in xs:
  sum_x += x
to
sum_x = sum(xs)
 
User avatar
ISayMoo
Posts: 2213
Joined: September 30th, 2015, 8:30 pm

Re: Python tricks

August 15th, 2019, 5:58 pm

ABOUT WILMOTT

PW by JB

Wilmott.com has been "Serving the Quantitative Finance Community" since 2001. Continued...


Twitter LinkedIn Instagram

JOBS BOARD

JOBS BOARD

Looking for a quant job, risk, algo trading,...? Browse jobs here...


GZIP: On