某人来自C的提问... [英] Question by someone coming from C...

查看:60
本文介绍了某人来自C的提问...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用Python编写这个应用程序,不确定最佳实践是什么将

be。


我想要一个bitfield全局日志记录级别,允许我打开和关闭特定的

调试模块。如果我在C中这样做,我只会使用

一些全局变量如下:


unsigned int debug_level = 0;

#define DEBUG_GENERAL 0x0001

#define DEBUG_CONFIG 0x0002

#define DEBUG_OPTIONS 0x0004

等等


所以我想我的问题是:


1.似乎没有办法像

其他语言那样定义全局常量?

2.在Python中使用位域的任何特殊巫术?


谢谢!

Skye

Writing this app in Python, not sure what the "best practice" would
be.

I want a bitfield global logging level that allows me to turn specific
debugging modules on and off. If I was doing this in C, I''d just use
some globals like:

unsigned int debug_level = 0;
#define DEBUG_GENERAL 0x0001
#define DEBUG_CONFIG 0x0002
#define DEBUG_OPTIONS 0x0004
etc etc

So I guess my questions are:

1. there doesn''t seem to be a way to define global constants like in
other languages?
2. any special voodoo to use bitfields in Python?

Thanks!
Skye

推荐答案

6月10日凌晨4点*,Skye< spo ... @ gmail.comwrote:
On Jun 10, 4:00*am, Skye <spo...@gmail.comwrote:

用Python编写这个应用程序,不确定最佳实践是什么将

be。


我想要一个bitfield全局日志记录级别,允许我打开和关闭特定的

调试模块。 *如果我在C中这样做,我只会使用

一些全局字符如下:


unsigned int debug_level = 0;

#define DEBUG_GENERAL 0x0001

#define DEBUG_CONFIG 0x0002

#define DEBUG_OPTIONS 0x0004

等等


所以我想我的问题是:


1.似乎没有办法定义全局常量,例如

其他语言?
Writing this app in Python, not sure what the "best practice" would
be.

I want a bitfield global logging level that allows me to turn specific
debugging modules on and off. *If I was doing this in C, I''d just use
some globals like:

unsigned int debug_level = 0;
#define DEBUG_GENERAL 0x0001
#define DEBUG_CONFIG 0x0002
#define DEBUG_OPTIONS 0x0004
etc etc

So I guess my questions are:

1. there doesn''t seem to be a way to define global constants like in
other languages?



全局变量是这样的:


- module.py -

someglobal = 1

def func():

打印someglobal

#这使得一些全局读取,

#任何尝试写给someglobal

#会创建一个新的局部变量。


def func2():

global someglobal
someglobal = 2

#这可以让你操纵全局

#变量

- -

Global variables is like this:

-- module.py --
someglobal = 1
def func():
print someglobal
# This makes someglobal readonly,
# any attempt to write to someglobal
# would create a new local variable.

def func2():
global someglobal
someglobal = 2
# this allows you to manipulate the global
# variable
-- --


2.在Python中使用位域的任何*特殊伏都教?
2. any *special voodoo to use bitfields in Python?



这在Python中不是一个好主意。在C中,可能值得节省一些内存几个字节的内存,特别是对于嵌入式应用程序,在python中它是不值得的,虚拟机是巨大的比较

节省了几个字节的空间。


好​​吧,可以在python中进行位域操作:
http://aspn.activestate.com/ASPN/Coo.../Recipe/ 113799 但是

它真的不值得它

That''s a not a good idea in Python. In C, it might worth saving some
few bytes of memory especially for embedded application, in python
it''s just not worth it, the virtual machine is way huge compared to
the few bytes of space saved.

OK, it is possible to do bitfield manipulation in python:
http://aspn.activestate.com/ASPN/Coo.../Recipe/113799 but
it''s not really worth it


>

谢谢!

Skye
>
Thanks!
Skye


好的,听起来不错。所以,如果不是比特字段,那么做一个好的Python-y

的方法呢?

在调试配置中翻转布尔值字典什么的?


Skye

OK, sounds good. So if not bitfields, what would be a good Python-y
way to do it?
Flip booleans in a "debug config" dictionary or something?

Skye


6月9日下午2:00,Skye< spo。 .. @ gmail.comwrote:
On Jun 9, 2:00 pm, Skye <spo...@gmail.comwrote:

用Python编写这个应用程序,不确定最佳实践是什么将

be。


我想要一个bitfield全局日志记录级别,允许我打开和关闭特定的

调试模块。如果我在C中这样做,我只会使用

一些全局变量如下:


unsigned int debug_level = 0;

#define DEBUG_GENERAL 0x0001

#define DEBUG_CONFIG 0x0002

#define DEBUG_OPTIONS 0x0004

等等


所以我想我的问题是:


1.似乎没有办法像

其他语言那样定义全局常量?

2.在Python中使用位域的任何特殊巫术?


谢谢!

Skye
Writing this app in Python, not sure what the "best practice" would
be.

I want a bitfield global logging level that allows me to turn specific
debugging modules on and off. If I was doing this in C, I''d just use
some globals like:

unsigned int debug_level = 0;
#define DEBUG_GENERAL 0x0001
#define DEBUG_CONFIG 0x0002
#define DEBUG_OPTIONS 0x0004
etc etc

So I guess my questions are:

1. there doesn''t seem to be a way to define global constants like in
other languages?
2. any special voodoo to use bitfields in Python?

Thanks!
Skye



没有传统方法来定义常量。如果你环顾四周可能有一些

技巧,但我已经编写了很长时间的Python

而且从来没有真正需要一个。副作用是你可以在运行时重新定义其中一个值。但是通过命名约定,用户将知道不会这样做,就像他们知道不要使用以_开头的对象的属性来混乱

。如果他们确实搞砸了

,那么,他们更好地知道他们在做什么。


我所知道的没有特殊的bitfield伏都教。 />

一般来说,命名约定与C中的命名约定相同(所有

上限):


FOO = 0x01

BAR = 0x02

BAZ = 0x04


val = FOO | BAR | BAZ


如果val& BAR:

#do bar stuff


唯一一次做这种事情(在python中)就是交互时

但是其他东西并不是用Python编写的。通常,

用于记录,只需使用标准记录模块:
http://docs.python.org/lib/module-logging.html

Matt

There is no conventional way to define a constant. There may be some
tricks if you look around, but I''ve been coding Python for a long time
and never actually needed one. The side effect is that you could
redefine one of the values at run-time. By naming convention though,
the user will know not to do that, just like they know not to mess
with attributes of an object that begin with "_". If they do mess with
it, well, they better know what they are doing.

There is no special bitfield voodoo that I know of.

Generally, naming conventions are the same as they would be in C (all
caps):

FOO = 0x01
BAR = 0x02
BAZ = 0x04

val = FOO | BAR | BAZ

if val & BAR:
#do bar stuff

The only time to do that sort of thing (in python) is when interacting
with something else that isn''t written in Python though. In general,
for logging, just use the standard logging module:
http://docs.python.org/lib/module-logging.html
Matt

这篇关于某人来自C的提问...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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