pythonbytes,
@pythonbytes@fosstodon.org avatar
Inspiredharvey,

@pythonbytes @brianokken @mkennedy I thought repr was supposed to be able to copy and paste back into python to recreate the object to debug, Str is for humans

mkennedy,
@mkennedy@fosstodon.org avatar

@Inspiredharvey @pythonbytes @brianokken In theory. But in practice, it’s sometimes yes, sometimes no from what I see. Have an object with 1M lines of text in a list which is a field? I doubt you want that to fully print the inner dict and scroll forever. Have a nice tuple? Sure.

mkennedy,
@mkennedy@fosstodon.org avatar

@Inspiredharvey @pythonbytes @brianokken Another consideration is when the full state of the object cannot be recreated from a constructor. For example, if internally it has a list of albums, but the only way to add an album would be to use some self.add_album(…) method. What does repr() have to been to recreate that? A multi-line initialize block of Python?

So my philosophy is it’d be nice if repr() output text that could recreate the object, but reality means sometimes it’s not possible.

Inspiredharvey,

@mkennedy @pythonbytes @brianokken I agree, in reality things are not always so simply constructed, or objects are too large to log, or some attributes are secrets. That said, where it is possible in the other 80% of cases, keeping the idea that the repr could be used to reconstruct the object can be very useful.

mkennedy,
@mkennedy@fosstodon.org avatar

@Inspiredharvey @pythonbytes @brianokken Agreed.

I wouldn’t hate seeing Python adopt a single expression initializer to make even more of these possible (but also to enhance comprehensions, etc). For example:

a = A(x=1, y2):
self.albums = [‘a1’, ‘a2’, ‘a3’…]
self.title = “Some title not available in constructor”

mkennedy,
@mkennedy@fosstodon.org avatar

@Inspiredharvey @pythonbytes @brianokken But Python is already complex enough likely :)

tonybaloney,
@tonybaloney@fosstodon.org avatar

@brianokken @pythonbytes @mkennedy I still don’t understand the point of repr (over str).

henryiii,
@henryiii@fosstodon.org avatar

@tonybaloney @brianokken @pythonbytes @mkennedy repr is the “programmer’s view”, while str is what it looks like as a string. You don’t have to implement str, it will default to calling repr. For example, a Card class could be have a repr of ‘Card(“Jack”)’ and a string “J”.

mkennedy,
@mkennedy@fosstodon.org avatar

@henryiii @tonybaloney @brianokken @pythonbytes That’s a good way to put it. The way I think of it is if I saw it in a debugging statement/window, would it be helpful?

Like users: list[user] = […] … then print(users), useful? It might be
[
User(email=“…”, id=“…”),
User(email=“…”, id=“…”)
]

and that’s it even though there are way more attributes.

  • All
  • Subscribed
  • Moderated
  • Favorites
  • python
  • DreamBathrooms
  • mdbf
  • ngwrru68w68
  • magazineikmin
  • thenastyranch
  • rosin
  • khanakhh
  • osvaldo12
  • Youngstown
  • slotface
  • Durango
  • kavyap
  • InstantRegret
  • tacticalgear
  • provamag3
  • ethstaker
  • cisconetworking
  • modclub
  • tester
  • GTA5RPClips
  • cubers
  • everett
  • normalnudes
  • megavids
  • Leos
  • anitta
  • JUstTest
  • lostlight
  • All magazines