what is the accepted style python for data objects -


this question has answer here:

what normal style data objects in python. lets have method gets customer somewhere (net, db, ....) type of object should return. see several choices:

  • a tuple
  • a dictionary
  • a class instance (are data classes 'normal')

i sure there others. doing first big python project , start out using best practices start

wow - surprised @ negative reaction question. maybe not being clear

i have lots of different data items want pass around code. user, product, customer, order,... (in fact nothing simpler obvious types of thing). have

def get_user():   return x 

what should x be. instance of class called user, dict, tuple...

there no methods associated objects, pure data

seems namedtuples way go

edit: how style

class product:    pass  ...  def get_product():    ... db read stuff    pr = product()    pr.name = dbthing[0]    pr.price = dbthing[1]    return pr 

is barf inducing or established style or odd or what? works. consumer side makes readable code

def xxx():   pr = get_product()   total = amount * pr.price 

for simple data records should think collections.namedtuple first, , use 1 of other options if that's not suitable reason. once know why it's not suitable suggests of others use. can think of collections.namedtuple being shortcut defining immutable "data classes".

taking database example, if you're using orm simplest records represented objects[*], , reason because have in common actions can perform on them such storing changes database. tutorial/documentation orm guide you. if you're using python db api directly rows sql queries come (initially) tuples, of course can once have them. database connector can provide ways manipulate them before call execute() returns them, example setting row factory in sqlite3.

taking "the net" example -- well, there many means of data interchange, 1 common example accessing api returns json data. in case there's not choice initially represent data in program same way structured json: lists , dictionaries containing lists, dictionaries, strings , numbers. again, can once have it. it's normal work is, it's normal re-structured else straight away. both personal preference , particular circumstances affect choose.

you should think of of these things available options. speaking use:

  • a tuple when each position has own meaning. that's why python returns "multiple values" function returning tuple, because different things might different types different meaning.
  • a dictionary when available keys vary record.
  • a data class when keys same every record. can use namedtuple immutable records.
  • a list or tuple when order important positions equivalent. observe there's little tension here between "tuples immutable, lists mutable" vs. "tuples heterogeneous data , lists homogeneous data". lean towards former i've seen sensible arguments latter, if you're asking how people in general make choice can't ignore that.

[*] well, tuples , dictionaries objects of course, mean objects other these data structures ;-)


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -