On Jul 16, 2008, at 4:43 PM, Tamas wrote:

>
> Hi,
>
> I am not a professional coder; hobby-like thing.
> I would like to encapsulate my table definitions in a new class:
>
> class MyTable( Table):
>  def __init__( self, metadata):
>      Table.__init__( self, my_table_name, metadata, col1, col2...)
>
> metadata = MetaData()
> table = MyTable( metadata)
>
> Traceback (most recent call last):
>  File "create_tables.py", line 11, in <module>
>    table = MyTable( metadata)
> TypeError: __call__() takes at least 3 arguments (2 given)
>
> *******
> I tested different things and I have the feeling that it is not
> possible to subclass the Table class.
> Is this true?
>
> If no: what is the mistake I make?
>
> If yes: why? does Table class have some special decoration or
> something like that?
> How do you avoid to define your table  in every script you write to
> use that table with its corresponding class?


its possible, but tricky since the creation of a Table is routed  
through a metaclass which ensures that only one instance of Table for  
a particular name within the MetaData exists, i.e.:


-> t1 = Table('t1', metadata, ...)
-> t2 = Table('t1', metadata, ...)
-> t1 is t2
True

So true subclassing here would require that you also subclass the  
metaclass to do what you want here, which is probably not a good idea  
since thats not public API stuff.

If its just a creational pattern you're looking for, I'd suggest using  
a def instead of a class:

def MyTable(metadata):
     return Table('myname', metadata, ...)

If there's other behavior you're looking for, let us know what it is  
and we'll see if we can accomodate you.



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to