Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Idiomatic Python way of naming a method that converts the object to another format

Tags:

python

idioms

I'm looking for what is the most idiomatic way of naming a method that for instance converts the data of a class to xml.

If I were doing this in Ruby I would make a method named to_xml for instance, but I've seen several different ways of doing this in Python. toxml, to_xml, xml and toXML, for different outputs than XML, but just extrapolating what it would look like for XML. And taken how you convert objects for native Python objects (str() etc) I don't really know what would be the idiomatic way.

Personally I would just continue and use to_xml since that's what I'm used to, but would that be unexpected to an experienced Python programmer?

like image 408
gaqzi Avatar asked Nov 22 '25 01:11

gaqzi


1 Answers

PEP8 recommends to_xml:

Use the function naming rules: lowercase with words separated by underscores as necessary to improve readability.

like image 59
Brian Cain Avatar answered Nov 24 '25 09:11

Brian Cain



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!