What is the accepted python style for data objects

What is the usual style for data objects in python. Suppose I have a method that receives from someone (net, DB, ....) what type of object I should return. I see several options:

  • motorcade
  • dictionary
  • class instance (data classes are "normal")

I am sure there are others. By doing my first big python project and therefore would like to start using best practices to get started.

Wow - surprised at the negative reaction to the question. Maybe not clear [/ p>

I have many different data elements that I want to pass to my code. User, product, customer, order, ... (in fact, they are nothing like this, but simpler with obvious types of things). so I

def get_user():
  return x

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

, ,

, namedtuples -

edit:

class product:
   pass

...

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

, ? .

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

collections.namedtuple , - . , , , , . collections.namedtuple " ".

, ORM, [*], , , . / ORM . API- Python , SQL- () , , , , , , . , , execute() , , factory sqlite3.

"" - , , - API, JSON. , , JSON: , , , . , , , . , , , - . , , .

. , :

  • , . Python " " , , .
  • , .
  • , . namedtuple .
  • , , . , " , " " , " ). , , , , , .

[*] , , , , , ; -)

+1

" " , .

, , - . , , , . , , .

- , . - .

namedtuples , , , , - -:

from collections import namedtuple

Product = namedtuple('Product', 'name price')

p = Product("some product", 10)

, , __unicode__, . , . namedtuple:

class Product(namedtuple('Product', 'name price')):
    def __unicode__(self):
        return "{} (${})".format(self.name, self.price)

- . , , . - , , - .

+1

namedtuples, . , , API . API-, .

, , , , . , "" , . , API, , , . , " ", " . , ++ Java, - .

0

Source: https://habr.com/ru/post/1536013/


All Articles