tag.get() is inconsistent

Bug #1678589 reported by Fake Name
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Beautiful Soup
Fix Released
Undecided
Unassigned

Bug Description

Basically, tag.get(<string>) is annoying to use.

If you call tag.get('class'), you get a list of attributes.
However, in all other cases, tag.get(other-string) returns a plain string.

This means, if you're ever dealing with a context where you can be trying to get variable tag attributes, you need to do a bunch of messy isinstance() cruft on the return value of tag.get().

It's probably too late to change, but tag.get() should either *always* return a list, or *always* return a string (and leave splitting the string into a list to the user).

Moving forward, I'd suggest implementing such behaviour as a new function, and depreciating the current tag.get().

Revision history for this message
Leonard Richardson (leonardr) wrote :

I sympathize with what you're saying but I'm not going to change the behavior of Tag.get(). There are a lot of things you might do with attributes and the simplest way to handle it overall is to model what actually goes on in an HTML document: some attributes, like class and rel, have a list of values, and others can only have one value.

In revision 445 I've added a convenience method called Tag.get_attribute_text, which will automatically join the values of multi-valued attributes into a single string, similar to how Tag.get_text joins the strings found inside a tag into a single string.

Changed in beautifulsoup:
status: New → Fix Committed
Revision history for this message
Fake Name (lemuix-2) wrote :

Personally, I'd prefer getting a list then a reconstituted string.

Revision history for this message
Leonard Richardson (leonardr) wrote :

Okay, revision 447 changes get_attribute_string to get_attribute_list.

Revision history for this message
Fake Name (lemuix-2) wrote :

Awesome!

Changed in beautifulsoup:
status: Fix Committed → Fix Released
Revision history for this message
Fake Name (lemuix-2) wrote :

Woot!

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.