Author Topic: My love affair with Python  (Read 2671 times)

0 Members and 1 Guest are viewing this topic.

Offline Kopachris

  • 28
  • send penguins
    • Steam
    • Twitter
My love affair with Python
Okay, I'm freaking loving Python right now.  It seems so... elegant.  What really won me over is that I can do something like this extremely easily:
Code: [Select]
class HeaderChunk:
  ...

class TextureChunk:
  ...

...

chunk_dict = {
  b'HDR2': HeaderChunk,
  b'OHDR': HeaderChunk,
  b'TXTR': TextureChunk,
  ...
}

while not eof:
  chunk_id = pof_file.read(4)
  this_chunk = chunk_dict[chunk_id]()
  ...
Without the filler ellipses, of course.

Way prettier than a bulky C-style switch-case.

Anyone around here dabble in Python?
----
My Bandcamp | Discord: Kopachris#0001 | My GitHub

 

Offline Luis Dias

  • 211
Re: My love affair with Python
Kinda reminds me some xkcd's posts.

 

Offline Kopachris

  • 28
  • send penguins
    • Steam
    • Twitter
Re: My love affair with Python
Kinda reminds me some xkcd's posts.

Code: [Select]
import antigravitywill open the user's default browser to this page in any standard Python distribution.
----
My Bandcamp | Discord: Kopachris#0001 | My GitHub

 
Re: My love affair with Python
Anyone around here dabble in Python?
Aye, learned it for work. Nothing overly complicated so far, but I did write a partially-structured 4-D interpolation routine at the end of my first week with Python :)

To me, the main advantages of Python are its open-sourceness (as opposed to MATLAB, the main competitor here at work) and the fact that you don't need to compile it (which saves a lot of time in bugfixing and general messing about).

As for the simplicity of Python coding... Idk, I have a feel that C++ can be just as simple, as long as you don't go hacking its object-oriented paradigm with pointers and stuff. A lot of more experienced coders than me seem to be doing this, and I still haven't got a clue why.

 

Offline Aardwolf

  • 211
  • Posts: 16,384
    • Minecraft
Re: My love affair with Python
Lolwut... you can't do **** in C++ without pointers. Well, you can, but you'd be stupid to avoid using pointers  :blah:

 

Offline The E

  • He's Ebeneezer Goode
  • 213
  • Nothing personal, just tech support.
    • Steam
    • Twitter
Re: My love affair with Python
OOP has nothing to say about pointers. In C++, they're kinda essential when trying to do polymorphism, IMHO.

Also, please remember that C++ has two ways of dealing with pointers, either via the C-style kind ( int* intpointer) or via C++ references ( void myFunc(int& a) {}).
If I'm just aching this can't go on
I came from chasing dreams to feel alone
There must be changes, miss to feel strong
I really need lifе to touch me
--Evergrey, Where August Mourns

 

Offline Kopachris

  • 28
  • send penguins
    • Steam
    • Twitter
Re: My love affair with Python
Lolwut... you can't do **** in C++ without pointers. Well, you can, but you'd be stupid to avoid using pointers  :blah:
In Python, basically everything is pointers.  For example:
Code: [Select]
foo = [0,1,1,2,3,5,8]
bar = foo
bar.append(13)
print(foo)
>>> [0,1,1,2,3,5,8,13]

Really useful if you know about that feature.  Really annoying if you don't.
----
My Bandcamp | Discord: Kopachris#0001 | My GitHub

 
Re: My love affair with Python
In C++, they're kinda essential when trying to do polymorphism, IMHO.

I see... Never got that far with C++ :)

In Python, basically everything is pointers.

One of the things I find annoying in Python is that because of this, you can pass basically anything to a function, there's no way it will go "Hey I didn't expect that data type in that position". Which can make for some pretty damn obscure bugs...

 

Offline Nuke

  • Ka-Boom!
  • 212
  • Mutants Worship Me
Re: My love affair with Python
lua has that problem too, and no seemingly fast way to verify data types. the surefire way is the type command, but then you have to do a string compare. you can check for nil, but then you have to worry about the wrong datatype. so you have to choose pretty early on to either make your function safe or fast. the other side of it is you can make functions more versatile. when i do classes i usually have one ultra-versatile constructor, and then a bunch of fast ones.

i never bothered learning pyhon. so far i can do everything i need to do with standalone lua.
I can no longer sit back and allow communist infiltration, communist indoctrination, communist subversion, and the international communist conspiracy to sap and impurify all of our precious bodily fluids.

Nuke's Scripting SVN

 

Offline Kopachris

  • 28
  • send penguins
    • Steam
    • Twitter
Re: My love affair with Python
In Python, basically everything is pointers.

One of the things I find annoying in Python is that because of this, you can pass basically anything to a function, there's no way it will go "Hey I didn't expect that data type in that position". Which can make for some pretty damn obscure bugs...
Code: [Select]
def Foo(x):
  """Creates a 2D array of length x."""
  if not isinstance(x, int):
    raise TypeError("Expected integer!")
  bar = [[] for i in range(x)]
  return bar

or

Code: [Select]
def Foo(x):
  """Creates a 2D array of length x."""
  x = int(x)  # Will raise exception if x can't become int
  bar = [[] for i in range(x)]
  return bar

Annoying to have to do that, I agree, but it's not hard.  I usually skip it and just keep track of my types on my own.
----
My Bandcamp | Discord: Kopachris#0001 | My GitHub