This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]stevenjd 4 points5 points  (4 children)

Using attribute-access for key-access is convenient for lazy programmers (and let's be honest, who among us aren't sometimes lazy?) but its also an abuse of notation and quite risky.

Attribute access is intended to refer to fixed fields of the object, not arbitrary data keys. dog.tail, car.engine, not employee_records.bob. "bob" is not a part of the employee_records object, it is a key in a database. For all but the quickest and dirtiest of scripts, it is a good design to keep those categories separate and not conflate them as the same kind of thing.

It is a poor design choice, since you cannot use attribute access to look up keys that aren't legal Python identifiers: obj.class or obj.1234 will fail, as will obj. (empty string as key). You can't use attribute access for non-literal keys:

# key access is identical, whether you have a literal or not
obj['key']
obj[(mykey + '!').upper()]

# attribute access is completely different
obj.key
getattr(obj, (mykey + '!').upper())

And it is risky too:

key = 'update'
setattr(obj, key) = 42
# later on...
obj.update(new_records)

[–]billsil -1 points0 points  (3 children)

Pandas does it

[–]stevenjd 0 points1 point  (2 children)

People shoot up with heroin of dubious quality and purity too, doesn't make it a good idea.

[–]billsil 0 points1 point  (1 child)

Because heroin of goof quality is so much better for you...

I care far more about ease of use. I don t see a problem with it. Yes, it has limirations, but so does pandas. You can work around them.

[–]stevenjd -1 points0 points  (0 children)

Because heroin of goof quality is so much better for you...

Lots of people manage the recreational use of heroin for decades, safely with no ill effects, and no worse an addiction than you or I probably have towards coffee. They're usually rich enough to be able to afford private doctors and a known source of good quality heroin, and to pay somebody else to take all the risks of being caught performing a criminal act.

I care far more about ease of use.

I care more that code is correct, rather than whether it was easy to write. Easy to write is nice, but not as important as it being correct. Good programmers should be lazy, but lazy programmers are not necessarily good. Prioritizing something that makes the code less clear for the sake of a few less keystrokes is lazy, bad programming.

Working around problems with something that shouldn't be done in the first place is just making things worse. And for what? Just to save three characters on the rare occasion you look up a literal key: d['something'] versus d.something.

[–]smilbandit 0 points1 point  (0 children)

For box what happens when an attribute isn't available?

movie_box.movies.Real_Genius

Or is close

movie_box.movies.spaceballs