2012-09-04 22 views
0
class FooTable(Base): 

    Id = Column(BIGINT, primary_key=True) 
    name = Column(VARCHAR(256), nullable=False) 
    username = Column(VARCHAR(256), nullable=False) 
    state = Column(VARCHAR(100), nullable=False) 
    city = Column(VARCHAR(256), nullable=False) 
    zip = Column(VARCHAR(100), nullable=False) 

我想打印出类Column的实例化参数。一般来说,python代码要串起来。例如:打印python上的类参数

for k, v in vars(FooTable).iteritems(): 
     print k, get_class_attr(v) 


>>> Id ["BIGINT", "primary_key=True"] 
>>> name ["VARCHAR(256)", "nullable=False"] 
    ... 

我试过检查模块,却发现它不支持它: http://docs.python.org/library/inspect.html#inspect.getmembers

感谢任何信息

+0

它是sqlalchemy?你想得到:'FooTable .__ table __。columns._data'(但没有访问私有属性)? – jfs

回答

0

您可以用打印类的属性:

print dir(Column) 

这是如果我正确理解你的问题

+0

确实。修复。 –

+0

抱歉,不是属性,调用类的参数 – eligro

0

attrs = [i for i in dir(obj) if not i.startswith('_')]

请记住,以_开头的属性通常被认为是“私有”的。下面是一个运行示例:

>>> [i for i in dir(4) if not i.startswith('_')] 
['conjugate', 'denominator', 'imag', 'numerator', 'real'] 
>>> [i for i in dir('') if not i.startswith('_')] 
['capitalize', 'center', ... 'split', 'splitlines', ... 'upper', 'zfill'] 
>>> class Foo: 
... a = 'Hello' 
... def __init__(self): 
...  pass 
... def hello(self): 
...  print 'Hello' 
... 
>>> [i for i in dir(Foo) if not i.startswith('_')] 
['a', 'hello'] 
+0

我的意思是如果a = column('foo','baz')和column是类,并且我想获得'foo'和'baz' – eligro

2

我建议使用__dict__然后filterting其输出为DIR不会使用元类工作之一。

def filterFunction(field): 
    ''' 
    This is a sample filtering function 
    ''' 
    name, value = field 
    return not name.startswith("_") 

for k, v in vars(FooTable).iteritems(): 
     print k, filter(filterFunction, v.__dict__.items()) 

对于这种情况对于下面的类

class X(): 
    foo = "bar" 
    baz = True 

我们的过滤器

filter(filterFunction, X.__dict__.items()) 

将返回

[('foo', 'bar'), ('baz', True)] 

要获得实例化的参数,我会检查是否领域名字在Column.__init__.im_self

+1

请再次阅读已编辑的问题。 OP想知道实例化参数。 – rantanplan

+0

在答案下面对应改变 –

+0

仍然没有。 OP想要知道在类的构造函数上在运行时传递的* actual *参数。不是构造函数参数既不是实例的属性。 – rantanplan

0

我认为你正在寻找这样的:

>>> class Foo: 
... def bar(a,b,c): 
...  pass 
... 
>>> x = Foo() 
>>> x.bar.func_code.co_varnames 
('a', 'b', 'c') 

这里是另一种尝试,我认为这不会你所需要的;尽管它有点复杂。

>>> class Foo: 
... a = 'b' 
... def bar(y,z): 
...  pass 
... 

>>> funcs = [(i,getattr(Foo,i).func_code.co_varnames) for i in dir(Foo) \ 
...   if hasattr(getattr(Foo,i),'func_code')] 
>>> 
>>> funcs 
[('bar', ('y', 'z'))] 

但是,如果你想知道传递给一类什么样的参数,然后从实例,使用__dict__

>>> class Foo: 
... a = '' 
... def __init__(self, b, c): 
...  self.a = b 
...  self.c = c 
... 
>>> funcs = [(i,getattr(Foo,i).func_code.co_varnames) for i in dir(Foo) if hasattr(getattr(Foo,i),'func_code')] 
>>> funcs 
[('__init__', ('self', 'b', 'c'))] 
>>> i = Foo(1,2) 
>>> i.__dict__ 
{'a': 1, 'c': 2} 
+0

不工作..注意,酒吧应该是类,而不是功能... – eligro

1

其实你在问什么,没有多大意义。 Burhan Khalid的答案很接近,但并不是真的。你想要实际调用者在实例化 类时传递的参数。不是函数的/构造函数的签名。

但是,这是班级的程序员应该自己做的书。即使如此,他也不会这样做。他会configure每个实例基于上通过 参数。

例如

class Foo(object): 
    def __init__(self, *args, **kwargs): 
     self.is_cool = kwargs.get('whatever', False) 

然后我会检查实例属性:

f = Foo() 
f.is_cool # False 

,我会尽量做到它的意义,这取决于这意味着我的实例。

但我真的不在乎通过的实际参数。我的实例 根据配置了自己的

你当然可以编写一个包装任何第三方类的类装饰器,并根据你的需要做 ,但这是一种开销,在这种情况下似乎不合理。