Forums

Welcome Guest

Pages: 1
Python Is Easy
OneMadGypsyPostOctober 29, 2018, 14:08
Moderator
Posts: 307
Registered:
November 12, 2017, 00:13
Normal topicPython Is Easy

I keep seeing "Python is easy" all over the place. Let me tell you right now from somebody that is not at all new to programming ... BULL FUCKING SHIT. It suffers from the same "not at all obvious" quirks that every other language suffers from and, as usual, those quirks are never documented. I will give you a perfect example

from struct import *
from collections import namedtuple

record = b'raymond   x32x12x08x01x08'

Student = namedtuple('Student', 'name serialnum school gradelevel')
Student._make(unpack('<10sHHb', record))
print(Student.name)

record is being cast as bytes for the purpose of making some quick bytes to work with. unpack is unpacking those bytes to the fields declared in the namedtuple instance. The '<10sHHb' part is just saying "use little endian(<), get a 10 character string(10s), then 2 unsigned shorts(HH) and finally a signed byte(b)". In other words it's defining a struct pattern. So, when you put all that together the results should be Student.name = raymond Student.serialnum = 4658 Student.school = 264 Student.grade = 8 However, try and print any of that and you get "object at 0x00333874" (as an example). Nowhere in the fucking docs does it explain that you need to do this

myStudent = Student._make(unpack('<10sHHb', record))
print(myStudent.name)

If it wasn't for the fact that I am not new to this shit it may have never struck me that Student is basically a class and make is basically a new instance. Meaning 'Student" does not exist as anything beyond a possibility no matter how many times you call make. You have to actually capture the result of make. One thing that makes this very confusing is this line

Student = namedtuple('Student', 'name serialnum school gradelevel')

Why would I think I am inventing a "possibility" when that line clearly depicts an instantiation?

Basically python has figured out a very confusing way to rewrite this

Student = SomeClass;
//Student = namedtuple('Student', 'name serialnum school gradelevel')

new Student(...);
//Student._make(unpack('<10sHHb', record))

and if you don't realize that, you probably won't come to the conclusion you need to do this

myStudent = new Student(...);
//myStudent = Student._make(unpack('<10sHHb', record))

I am not at a "fuck python" level though. Not even close. I just ordered a Raspberry Pi 3 B+ ultimate kit so, "fuck python" isn't even an option. Even if it was an option I'm not mad as hell yet ... and maybe I wont ever be as I work through these little intricacies and become proficient in the language. "Python is easy" though is some bullshit. Sure it has very simple syntax (that is completely different from basically every other OOP language) and it has some good modules* that make things very easy (once you figure out how the fuck to use it) but, "easy" in general? Nope.

*good modules: that unpack feature is exciting to me. Being able to use a few letters to define a struct and then dumping in some bytes is going to come in VERY handy...

...off to create a python BSP parser, like you didn't see that coming, right?

------

I also bought this 4gig Rock 64 Board. It's .2 Ghz faster than the Pi with quadruple the ram and has a dedicated GPU for 4$ more than I paid for the board portion of my Pi kit. Both run Debian and both support Python. I'm tempted to turn it into a Quake server, plug it into the router at work and bury it in the wire garden behind the desk. I bet I could get away with it for a very long time. Maybe forever. Including after I inevitably quit.

Image

https://all3dp.com/1/single-board-computer-raspberry-pi-alternative/#rock64-media-board

SpiritPostDecember 28, 2018, 06:40
Rookie
Posts: 1
Registered:
December 23, 2018, 19:02
Normal topicReply To: Python Is Easy

Nooo, don't believe this guy! ;) Python is quite easy to grasp and well documented. If you come from another language you will need to switch to a slightly different mindset for some things though.

How useless would named tuples be if you would have to declare the structure for every instance you want to create! Also, it is totally well documented, you either did not read or not understand it! :P

https://docs.python.org/3/library/collections.html#collections.namedtuple says

Returns a new tuple subclass named typename. The new subclass is used to create tuple-like objects that have fields accessible by attribute lookup as well as being indexable and iterable. Instances of the subclass also have a helpful docstring (with typename and field_names) and a helpful __repr__() method which lists the tuple contents in a name=value format.

Then you say:

If it wasn’t for the fact that I am not new to this shit it may have never struck me that Student is basically a class and make is basically a new instance.

https://docs.python.org/3/library/collections.html#collections.somenamedtuple._make

Class method that makes a new instance from an existing sequence or iterable.

Use type(something) to find out the type of something before wasting too much time trying to guess!

And if you don't want to write a "full" class, check out the new fancy dataclasses: https://docs.python.org/3/library/dataclasses.html

PS:

Student = SomeClass;
new Student(...);

looks like a superweird pattern to me. To me it looks like the result would still be an anonymous object of SomeClass?

OneMadGypsyPostDecember 28, 2018, 14:28
Moderator
Posts: 307
Registered:
November 12, 2017, 00:13
Normal topicReply To: Python Is Easy

I can't really argue at this point. I've gotten a lot better at Python since I originally wrote this. As far as the very last thing you wrote goes. I was writing pseudo code but, the concept is unnecessary code, because it makes a var that is essentially renaming a namespace.

var Student:Class = some.namespace.SomeClass;
var student:Student = new Student();

...is essentially what I was writing because that's exactly what it seemed like python was doing. Actually, that still seems like what python is doing. Where line 1 would be inventing the tuple and line 2 would be the make. I had only been programming in python for like 1 day when I wrote my OP. less than a week later I was here

you can cick this image for full-size[su_lightbox type="image" src="https://i.imgur.com/uvsKnWm.png"]
Image
[/su_lightbox]

...and of course there is a lot of functionality here that an image cannot express. My little PyQC Editor is entirely functional with only 2 exceptions.

1) There are no "project" options
2) The terminal is half-baked

I always get annoyed at a language if I don't figure it all out in 5 minutes :D and then I tend to excel at it. You should have seen me when I decided to buy an ass-load of electronic components, read the dictionary definition of what each thing does and then create a circuit :D :P . It was so fucked. I didn't even bother to learn Ohms law. In my defense, I knew ahead of time that the shit wasn't going to work. I just wanted to try. If I can find the picture of my first circuit I'll post it. It is possibly the most ridiculous thing you have ever seen. If I was trying to make a led destroyer then I did a great job with way too many components. :D The worst part was I misunderstood how a breadboard worked and I thought I had to manually connect things. I thought the board was just a bunch of holes to make things easy. So I was cramming 2 leads in every hole I used and shorting things out like crazy due to the "invisible" connections being made ... LMAO, DUMB AS FUCK! I have learned much since then and some of what I was guessing and figuring about wasn't wrong. In some small ways you could say that it was somewhat impressive for not knowing any very important thing about electronics but, overall it was a disaster. A fun disaster.

-------

We have pretty syntax highlighting on this forum and you can use it too ... :D
You have to manually escape less-than and open square-bracket, though - cause html and bbcode

Pages: 1
Page loaded in: 0.034 seconds.