Admiral Patrick@dubvee.org to Programmer Humor@programming.devEnglish · 4 days agoexitdubvee.orgexternal-linkmessage-square24fedilinkarrow-up11arrow-down10
arrow-up11arrow-down1external-linkexitdubvee.orgAdmiral Patrick@dubvee.org to Programmer Humor@programming.devEnglish · 4 days agomessage-square24fedilink
minus-squareRapidCatLauncher@lemmy.calinkfedilinkEnglisharrow-up0·4 days agoThat just gave me the idea that it would be fun to inspect exit a little. Which led me down this path: >>> repr(exit) 'Use exit() or Ctrl-Z plus Return to exit' >>> dir(exit) [(...), 'eof', 'name'] >>> exit.eof, exit.name ('Ctrl-Z plus Return', 'exit') Okay, cool, the “Use exit() etc.” blurb appears because it’s the function’s repr, and the string is assembled from its name and eof properties. Now let’s try to make our own: >>> exit.__class__ <class '_sitebuiltins.Quitter'> >>> gtfo = exit.__class__() TypeError: Quitter.__init__() missing 2 required positional arguments: 'name' and 'eof' Oh Python, you shouldn’t have. >>> gtfo = exit.__class__("a big puff of smoke", "a sneaky skedaddle") >>> gtfo Use a big puff of smoke() or a sneaky skedaddle to exit Beauty!
minus-squaresquaresinger@lemmy.worldlinkfedilinkarrow-up0·2 days agoDoes gtfo() then work as expected?
minus-squarelengau@midwest.sociallinkfedilinkarrow-up0·2 days agoObviously they need to make exit’s repr method raise a SystemExit
minus-squareMia@lemmy.dbzer0.comlinkfedilinkarrow-up0·3 days agoIt never occurred me to inspect this, nice find.
That just gave me the idea that it would be fun to inspect
exit
a little.Which led me down this path:
>>> repr(exit) 'Use exit() or Ctrl-Z plus Return to exit' >>> dir(exit) [(...), 'eof', 'name'] >>> exit.eof, exit.name ('Ctrl-Z plus Return', 'exit')
Okay, cool, the “Use exit() etc.” blurb appears because it’s the function’s
repr
, and the string is assembled from itsname
andeof
properties.Now let’s try to make our own:
>>> exit.__class__ <class '_sitebuiltins.Quitter'> >>> gtfo = exit.__class__() TypeError: Quitter.__init__() missing 2 required positional arguments: 'name' and 'eof'
Oh Python, you shouldn’t have.
>>> gtfo = exit.__class__("a big puff of smoke", "a sneaky skedaddle") >>> gtfo Use a big puff of smoke() or a sneaky skedaddle to exit
Beauty!
Does gtfo() then work as expected?
Yup
Obviously they need to make
exit
’s repr method raise aSystemExit
It never occurred me to inspect this, nice find.