你用什么作为Python的符号? [英] What do you use as symbols for Python ?

查看:71
本文介绍了你用什么作为Python的符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当你在程序中需要一些符号时,你在Python中使用了什么?


例如,一个对象得到一个状态。如果

表示为符号,则此状态更具可读性,例如打开,关闭,错误。

通常,在C或C ++中,我会用枚举:

enum OBJECT_STATE

{

打开,关闭,错误

}


在CAML或Haskell中我会使用联合类型:


类型ObjectState = Opened |关闭|错误


在Ruby中我会使用符号:


object.state =:open

object.state =:关闭

object.state =:错误


....但我不知道在Python中使用什么!


谢谢,


Pierre

When you need some symbols in your program, what do you use in Python ?

For example, an object get a state. This state is more readable if
expressed as a symbols, for example "opened", "closed", "error".
Typically, in C or C++, I would use an enum for that:
enum OBJECT_STATE
{
opened, closed, error
}

In CAML or Haskell I would use the union types:

type ObjectState = Opened | Closed | Error

In Ruby I would use the symbols :

object.state = :opened
object.state = :closed
object.state = :error

.... but I don''t know what to use in Python !

Thanks,

Pierre

推荐答案

Pierre Barbier de Reuille写道:
Pierre Barbier de Reuille wrote:
当你在程序中需要一些符号时,你在Python中使用了什么?

例如,一个对象获得一个状态。如果
表示为符号,则此状态更具可读性,例如打开,关闭,错误。
通常,在C或C ++中,我会使用枚举:
enum OBJECT_STATE
{
打开,关闭,错误
}
When you need some symbols in your program, what do you use in Python ?

For example, an object get a state. This state is more readable if
expressed as a symbols, for example "opened", "closed", "error".
Typically, in C or C++, I would use an enum for that:
enum OBJECT_STATE
{
opened, closed, error
}




OPENED,CLOSED,ERROR = range( 3)


object.state = OPENED


-

Erik Max Francis&& ma*@alcyone.com && http://www.alcyone.com/max/

美国加利福尼亚州圣何塞市&& 37 20 N 121 53 W&& AIM erikmaxfrancis

无论谁命名为颈缩都是一个很差的解剖判断。

- 格劳乔·马克思



OPENED, CLOSED, ERROR = range(3)

object.state = OPENED

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
Whoever named it necking was a poor judge of anatomy.
-- Groucho Marx


" Erik Max Francis < ma*@alcyone.com>写道:
"Erik Max Francis" <ma*@alcyone.com> wrote:
Pierre Barbier de Reuille写道:
Pierre Barbier de Reuille wrote:
当你在程序中需要一些符号时,你在Python中使用了什么? />
例如,一个对象获得一个状态。如果
表示为符号,则此状态更具可读性,例如打开,关闭,错误。
通常,在C或C ++中,我会使用枚举:
enum OBJECT_STATE
{
打开,关闭,错误
}
When you need some symbols in your program, what do you use in Python ?

For example, an object get a state. This state is more readable if
expressed as a symbols, for example "opened", "closed", "error".
Typically, in C or C++, I would use an enum for that:
enum OBJECT_STATE
{
opened, closed, error
}



OPENED,CLOSED,ERROR = range(3)

object.state = OPENED



OPENED, CLOSED, ERROR = range(3)

object.state = OPENED




或者如果你想要更接近实际枚举的东西,有几个

食谱食谱。
http://aspn.activestate .com / ASPN / Coo ... / Recipe / 413486 似乎

根据收视率相当不错。


George



Or if you want something closer to real enumerations, there are several
recipes in the cookbook.
http://aspn.activestate.com/ASPN/Coo.../Recipe/413486 seems to
be pretty good according to the ratings.

George


Pierre Barbier de Reuille写道:
Pierre Barbier de Reuille wrote:
当你在程序中需要一些符号时,你在Python中使用了什么?

例如,一个对象获得一个状态。如果
表示为符号,则此状态更具可读性,例如打开,关闭,错误。
通常,在C或C ++中,我会使用枚举:
枚举OBJECT_STATE
{
打开,关闭,错误
}

在CAML或Haskell中我会使用联合类型:

type ObjectState =已打开|关闭|错误

在Ruby中我会使用符号:

object.state =:open
object.state =:closed
object.state =:错误

...但我不知道在Python中使用什么!
When you need some symbols in your program, what do you use in Python ?

For example, an object get a state. This state is more readable if
expressed as a symbols, for example "opened", "closed", "error".
Typically, in C or C++, I would use an enum for that:
enum OBJECT_STATE
{
opened, closed, error
}

In CAML or Haskell I would use the union types:

type ObjectState = Opened | Closed | Error

In Ruby I would use the symbols :

object.state = :opened
object.state = :closed
object.state = :error

... but I don''t know what to use in Python !




取决于工作...如果我需要做位掩码操作,我会使用

整数标志。如果我希望符号是人类可读的,我将使用

字符串。但是Python中的所有内容都是一个对象,你可以使用

看起来合适......


因为我们处于''状态''例如,这里是一种可能的状态模式

实现:


类MyObject(对象):

def __init __(self,姓名):

self.name = name

self .__ class__ = ClosedState


state = property(fget = lambda self: self .__ class__)


def open(self,arg):

如果arg == 1:

self .__ class__ = OpenedState

else:

self .__ class__ = ErrorState


def close(self):

self .__ class__ = ClosedState

class OpenedState(MyObject):pass

class ClosedState(MyObject):pass

class ErrorState(MyObject):pass


m = MyObject(''toto'')

断言m.state是ClosedState

m.open(1)

断言m.state是OpenedState

m.close()

断言m .state是ClosedState

m.open(2)

断言m.state是错误状态


我做了状态''假的''对象,但是你可以通过在基类中定义默认方法和/或
覆盖''state''子类中的适当方法来使这成为一个真正的状态

模式实现。


HTH

-

bruno desthuilliers

python -c" print'' @''。join([''。''。join([w [:: - 1] for p in p.split(''。'')])for

p in'' o **** @ xiludom.gro''。split(''''')])"



Depends on the job... If I need to do bitmask operations, I''ll use
integer flags. If I want the symbol to be human-readable, I''ll use
strings. But everything in Python being an object, you can use whatever
seems appropriate....

Since we''re in a ''state'' exemple, here''s a possible state pattern
implementation:

class MyObject(object):
def __init__(self, name):
self.name = name
self.__class__ = ClosedState

state = property(fget=lambda self: self.__class__)

def open(self, arg):
if arg == 1:
self.__class__ = OpenedState
else:
self.__class__ = ErrorState

def close(self):
self.__class__ = ClosedState
class OpenedState(MyObject):pass
class ClosedState(MyObject):pass
class ErrorState(MyObject):pass

m = MyObject(''toto'')
assert m.state is ClosedState
m.open(1)
assert m.state is OpenedState
m.close()
assert m.state is ClosedState
m.open(2)
assert m.state is ErrorState

I made states ''dummy'' objects, but you could make this a real state
pattern implementation by defining default methods in the base class and
overriding appropriate methods in the ''state'' subclasses.

HTH
--
bruno desthuilliers
python -c "print ''@''.join([''.''.join([w[::-1] for w in p.split(''.'')]) for
p in ''o****@xiludom.gro''.split(''@'')])"


这篇关于你用什么作为Python的符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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