类似Struct的类的工厂 [英] Factory for Struct-like classes

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

问题描述

您好,


我希望能够做到这样的事情:


员工=结构(姓名,薪水)


然后:


john =员工(''john doe'',34000)

print john.salary


基本上,Employee = Struct(名称,工资)应相当于:


类员工(对象):

def __init __(自我,姓名,工资):

self.name =姓名

self.salary =工资


Ruby''''Scruct''类( http:// ruby-doc.org/core/classes/Struct.html

这样做。我想它可以用''exec''完成,但有更多的

Pythonic方式吗?


提前谢谢


PS我知道这种常见的模式:


类结构:

def __init __(自我,**条目):

self .__ dict __。更新(条目)


允许:


john = Struct(name =''john doe' ',薪水= 34000)

打印john.salary


但我要求的更为一般。

Hello,

I want to be able to do something like this:

Employee = Struct(name, salary)

And then:

john = Employee(''john doe'', 34000)
print john.salary

Basically, Employee = Struct(name, salary) should be equivalent to:

class Employee(object):
def __init__(self, name, salary):
self.name = name
self.salary = salary

Ruby''s ''Scruct'' class (http://ruby-doc.org/core/classes/Struct.html)
does this. I suppose it can be done with ''exec'', but is there a more
Pythonic way ?

Thanks in advance

P.S. I''m aware of this common "pattern":

class Struct:
def __init__(self, **entries):
self.__dict__.update(entries)

Which allows:

john = Struct(name=''john doe'', salary=34000)
print john.salary

But what I''m asking for is somewhat more general.

推荐答案

8月13日下午6:43,eliben< eli ... @ gmail.comwrote:
On Aug 13, 6:43 pm, eliben <eli...@gmail.comwrote:

你好,


我希望能够做到这样的事情:


员工=结构(姓名,薪水)


然后:


john =员工(''john doe'',34000)

print john.salary


基本上,Employee = Struct(名称,工资)应相当于:


类员工(对象):

def __init __(自我,姓名,工资):

self.name = name

self.salary =薪水


Ruby''''Scruct''类( http://ruby-doc.org/core/classes/Struct.html

这样做。我想它可以用''exec''完成,但有更多的

Pythonic方式吗?


提前谢谢


PS我知道这种常见的模式:


类结构:

def __init __(自我,**条目):

self .__ dict __。更新(条目)


允许:


john = Struct(name =''john doe' ',薪水= 34000)

打印john.salary


但我要求的更为一般。
Hello,

I want to be able to do something like this:

Employee = Struct(name, salary)

And then:

john = Employee(''john doe'', 34000)
print john.salary

Basically, Employee = Struct(name, salary) should be equivalent to:

class Employee(object):
def __init__(self, name, salary):
self.name = name
self.salary = salary

Ruby''s ''Scruct'' class (http://ruby-doc.org/core/classes/Struct.html)
does this. I suppose it can be done with ''exec'', but is there a more
Pythonic way ?

Thanks in advance

P.S. I''m aware of this common "pattern":

class Struct:
def __init__(self, **entries):
self.__dict__.update(entries)

Which allows:

john = Struct(name=''john doe'', salary=34000)
print john.salary

But what I''m asking for is somewhat more general.



NamedTuples: http ://code.activestate.com/recipes/303439/


eliben写道:
eliben wrote:

Ruby''''Scruct''类( http:// ruby-doc.org/core/classes/Struct.html

这样做。我想它可以用''exec''完成,但有更多的

Pythonic方式吗?
Ruby''s ''Scruct'' class (http://ruby-doc.org/core/classes/Struct.html)
does this. I suppose it can be done with ''exec'', but is there a more
Pythonic way ?



尝试命名为tuple http://code.activestate.com/recipes/500261/

命名的元组实现是Python 2.6和3.0的一部分。对于较旧的

版本的Python使用来自activestate的配方。


Christian


Try named tuple http://code.activestate.com/recipes/500261/

A named tuple implementation is part of Python 2.6 and 3.0. For older
versions of Python use the recipe from activestate.

Christian


8月13日,7:30 * pm,Christian Heimes< li ... @ cheimes.dewrote:
On Aug 13, 7:30*pm, Christian Heimes <li...@cheimes.dewrote:

eliben写道:
eliben wrote:

Ruby''''Scruct''类( http://ruby-doc.org/core/classes/Struct.html

执行此操作。我想它可以用''exec''完成,但有更多的

Pythonic方式吗?
Ruby''s ''Scruct'' class (http://ruby-doc.org/core/classes/Struct.html)
does this. I suppose it can be done with ''exec'', but is there a more
Pythonic way ?



尝试命名为tuplehttp://code.activestate.com/recipes/500261/


命名的元组实现是其中的一部分Python 2.6和3.0。对于旧的

版本的Python使用来自activestate的食谱。


Christian


Try named tuplehttp://code.activestate.com/recipes/500261/

A named tuple implementation is part of Python 2.6 and 3.0. For older
versions of Python use the recipe from activestate.

Christian



谢谢Christian - 这正是我一直在寻找的。

有些想法...


1)我看到这是用exec完成的,所以没有更多的pythonic

方式。


2)将字段定义为单个字符串是很奇怪的。为什么不使用

** kwargs呢?

Thanks Christian - this is exactly what I''ve been looking for.
Some thoughts...

1) I see this is done with exec anyway, so there''s no more pythonic
way.

2) The definition of fields as a single string is weird. Why not use
**kwargs instead ?


这篇关于类似Struct的类的工厂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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