1

Background: I'm using this guide; Pony Orm. It tells me to create an object (db = Database()), then create a class that inherits from db (class Person(db.Entity)....). As far as I can tell, Person is inheriting from an instance - I didn't even know that was possible. If it is possible, I don't know how to put the Person class in another file, since all entities would need the same db object, but I don't instantiate the db object until runtime (yet I'm creating them at design time). I feel like I'm missing something fundamental here.

db = Database()
class Person(db.Entity):
    name = Required(str)
    age = Required(int)
    cars = Set('Car')

Questions: (1st) In the example given, is Person really inheriting from an instance(db) or is something else going on? (2nd) How would I put the Person (and other classes) in its own file and share the db instance?

Note: I'm running Python 3.4.

[EDITs]

print(type(db.Entity)) # yields: <class 'pony.orm.core.EntityMeta'>
SteveJ
  • 3,034
  • 2
  • 27
  • 47
  • While `db` is an instance, `db.Entity` could be either an instance property or a class property. It seems likely that `db.Entity` is an instance property that is a dynamic class which references other instance properties. It's hard to pick this apart without diving into the source code, but a `print(type(db.Entity))` may reveal something. – Jared Goguen Jun 20 '17 at 23:07
  • 1
    Sharing the instance of `db` may be as simple as instantiating it in one file and importing it in others. – Jared Goguen Jun 20 '17 at 23:08
  • @JaredGoguen thank you for the response...so would that file be something like (db_creator), that all of my entities import, and the helper class that uses the enities? – SteveJ Jun 20 '17 at 23:14
  • Yeah, something like that. You might have to take care that each import isn't instantiating a new `db` object (I don't think it will) but this is easy to check. – Jared Goguen Jun 20 '17 at 23:18

1 Answers1

4
  1. Of course it's not possible to inherit from an instance. db may be an instance of Database, but db.Entity (which you're inheriting from) is very much a class. If you take a look at the source code you can see that it's a dynamically created class:

    self.Entity = type.__new__(EntityMeta, 'Entity', (Entity,), {})

  2. If you don't instantiate the db variable until runtime, this is tricky. You have to choose: either instantiate db at program startup and define your Person class as usual, or postpone the class definition until the db instance is created. Generally speaking it's never a good idea to create classes during runtime, so I would recommend instantiating db right away.

    Splitting this into multiple files however is easy. In file A.py you instantiate the db:

    db = Database()

    And in B.py you simply import A:

    from A import db class Person(db.Entity):

Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
  • Technically, [it is possible](https://stackoverflow.com/a/42361402/5014455) to inherit from an instance that isn't a class (all classes are instances of a metaclass, after all!), but it isn't particularly useful. – juanpa.arrivillaga Jun 20 '17 at 23:18
  • Wow, I feel like I will never master Python - thank you for the quick response. I'll use your import suggestion. – SteveJ Jun 20 '17 at 23:19