类声明快捷方式 [英] class declaration shortcut

查看:63
本文介绍了类声明快捷方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 www.rubyclr.com 中看到了一段代码片段,其中包含显示如何简单地声明一个类与c#中的等效代码进行比较。

我想知道是否有任何方法可以在Python中模拟它。


代码如下:


Person = struct.new(:name,:birthday,:children)


我尝试过类似的东西,但它与我想要的东西没什么关系:


def klass(table,* args):

cls = new.classobj(table,(),{})

for i in args:

setattr(cls,i,i)

返回cls


但上面的内容不是我想要的。

我想我应该找到一种方法将构造函数代码包含在

这个函数,但我不知道这是否可行。

另外,我想知道是否有办法使用变量名来

创建一个具有相同名称的类(如&qu ot;人以上。


嗯,如果有人有想法,我想知道...


Luis

I''ve come across a code snippet in www.rubyclr.com where they show how
easy it is to declare a class compared to equivalent code in c#.
I wonder if there is any way to emulate this in Python.

The code is as follows:

Person = struct.new( :name, :birthday, :children)

I tried something like this, but it''s nothing close to what I''d like:

def klass(table, *args):
cls = new.classobj(table, (), {})
for i in args:
setattr(cls, i, i)
return cls

But this above is not what I want.
I guess I should find a way to include the constructor code inside
this function, but I don''t know if this is possible.
Also, I wonder if there is a way to use the variable name in order to
create a class with the same name (as in "Person"above).

Well, if anyone has an idea, I''d like to know...

Luis

推荐答案

LuisM.Gonzálezéécrit:
Luis M. González a écrit :

I'在 www.rubyclr.com 中遇到了代码片段,其中显示了如何

很容易声明一个类与c#中的等效代码相比。

我想知道是否有任何方法可以在Python中模拟它。


代码如下:


Person = struct.new(:name,:birthday,:children)
I''ve come across a code snippet in www.rubyclr.com where they show how
easy it is to declare a class compared to equivalent code in c#.
I wonder if there is any way to emulate this in Python.

The code is as follows:

Person = struct.new( :name, :birthday, :children)



s / struct / Struct /

s/struct/Struct/


我尝试过类似的东西,但它与我想要的东西没什么关系:

def klass(table,* args):

cls = new.classobj(table,(),{})

for i in args:

setattr(cls,i,i)

返回cls


但是上面的内容不是我想要的。

我想我应该找到一种方法来包含构造函数代码

这个功能,但我不知道这是否可行。

另外,我想知道是否有办法使用变量名来为

创建一个具有相同名称的类(如上面的人物)。


好​​吧,如果有人有想法,我想知道......
I tried something like this, but it''s nothing close to what I''d like:

def klass(table, *args):
cls = new.classobj(table, (), {})
for i in args:
setattr(cls, i, i)
return cls

But this above is not what I want.
I guess I should find a way to include the constructor code inside
this function, but I don''t know if this is possible.
Also, I wonder if there is a way to use the variable name in order to
create a class with the same name (as in "Person"above).

Well, if anyone has an idea, I''d like to know...



这里是*非常* Q& D尝试 - 这并没有解决名称问题:

def Struct(name,* attribs):

args ="," .join("%s = None" at at at at at at at at at at at at at at at at at at at at at at at at $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $(
代表属性attrs)

source =("""

class%s(对象):

def __init__ (自我,%s):

%s

""" .strip())%(name,args,body)

#print source

code = compile(source,''dummy'',''single'')

exec代码

返回当地人()[姓名]


但请注意,我会立刻解雇任何使用这种令人厌恶的人在

生产代码中。

Here''s a *very* Q&D attempt - that doesn''t solve the name problem:
def Struct(name, *attribs):
args = ", ".join("%s=None" % attr for attr in attribs)
body = "\n ".join("self.%s = %s" % (attr, attr) \
for attr in attribs)
source = ("""
class %s(object):
def __init__(self, %s):
%s
""".strip()) % (name, args, body)
#print source
code = compile(source, ''dummy'', ''single'')
exec code
return locals()[name]

But note that I''d immediatly fire anyone using such an abomination in
production code.


LuisM.González写道:
Luis M. González wrote:

我在 www.rubyclr.com 他们在那里展示如何轻松宣布一个班级比较使用c#中的等效代码。

我想知道是否有任何方法可以模拟这在Python中。


代码如下:

Person = struct.new(:name,:birthday,:children)
I''ve come across a code snippet in www.rubyclr.com where they show how
easy it is to declare a class compared to equivalent code in c#.
I wonder if there is any way to emulate this in Python.

The code is as follows:

Person = struct.new( :name, :birthday, :children)



怎么样::


class Person(Record):

__slots__ =' 'name'',''birthday'',''children''


您可以使用类似::


的人=人(''Steve'',''4月25日'',[])

断言person.name ==''Steve''

断言person.birthday ==''4月25日''

断言不是person.children


这就是你要找的东西吗?如果是这样,Record

类的配方就在这里:

http://aspn.activestate.com/ASPN/Coo.../Recipe/502237


STeVe

How about something like::

class Person(Record):
__slots__ = ''name'', ''birthday'', ''children''

You can then use the class like::

person = Person(''Steve'', ''April 25'', [])
assert person.name == ''Steve''
assert person.birthday == ''April 25''
assert not person.children

Is that what you were looking for? If so, the recipe for the Record
class is here:

http://aspn.activestate.com/ASPN/Coo.../Recipe/502237

STeVe


2月28日下午6:21,Steven Bethard< steven.beth ... @ gmail.comwrote:
On Feb 28, 6:21 pm, Steven Bethard <steven.beth...@gmail.comwrote:

LuisM.González写道:
Luis M. González wrote:

我在www.rubyclr.com上看到了一个代码片段,它们显示了

很容易声明一个类与c#中的等效代码相比。

我想知道是否有任何方法可以在Python中模拟它。
I''ve come across a code snippet inwww.rubyclr.comwhere they show how
easy it is to declare a class compared to equivalent code in c#.
I wonder if there is any way to emulate this in Python.


代码如下:
The code is as follows:


Person = struct。 new(:name,:birthday,:children)
Person = struct.new( :name, :birthday, :children)



怎么样::


class Person(Record) :

__slots__ =''name'',''birthday'',''children''


然后你可以使用类似::


person =人(''Steve'',''4月25日'',[])

断言person.name ==''Steve''

断言person.birthday ==''4月25日''

断言不是person.children


这就是你的样子寻找?如果是这样,Record

类的配方就在这里:

http://aspn.activestate.com/ASPN/Coo.../Recipe/502237


STeVe


How about something like::

class Person(Record):
__slots__ = ''name'', ''birthday'', ''children''

You can then use the class like::

person = Person(''Steve'', ''April 25'', [])
assert person.name == ''Steve''
assert person.birthday == ''April 25''
assert not person.children

Is that what you were looking for? If so, the recipe for the Record
class is here:

http://aspn.activestate.com/ASPN/Coo.../Recipe/502237

STeVe




嗯...不是真的。

上面的代码应该是写这个的更短的方式:


类人物:

def __init __(自我,姓名,生日,孩子):

self.name = name

self.birthday =生日

self.children =孩子


所以这个问题的目的是找到一种方法来模拟这个

a单行和最小打字。


这里有一些问题:

1)如何获取变量名称(在这种情况下,Person)成为该类的

名称而没有明确表示它。

2)如何输入未包含在引号之间的属性名称。我能做到的唯一

方式是将它们作为字符串文字输入。


并不是我迫切需要它,但我''我只是好奇...


路易斯



Hmmm... not really.
The code above is supposed to be a shorter way of writing this:

class Person:
def __init__(self, name, birthday, children):
self.name = name
self.birthday = birthday
self.children = children

So the purpose of this question is finding a way to emulate this with
a single line and minimal typing.

There are a few problems here:
1) How to get the variable name (in this case "Person") become the
name of the class without explicity indicating it.
2) How to enter attribute names not enclosed between quotes. The only
way I can do it is by entering them as string literals.

It''s not that I desperately need it, but I''m just curious about it...

Luis


这篇关于类声明快捷方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆