小编Sil*_*ost的帖子

如何检查函数的参数?

我有这样定义的函数:

def f1 (a, b, c = None, d = None):
    .....
Run Code Online (Sandbox Code Playgroud)

我如何检查a,b不等于某个值.我想检查它们不是空字符串,""或者" "

思考像.

arguments = locals()
for item in arguments:
    check_attribute(item, arguments[item])
Run Code Online (Sandbox Code Playgroud)

然后检查参数是否不是""," ".但在这种情况下,它也会尝试检查None值(我不想做的事情).

python

1
推荐指数
2
解决办法
4485
查看次数

寻找PHP的str_split()替换

假设我有一小段代码:

<?php 
$tmp = str_split('hello world!',2);
// $tmp will now be: array('he','ll','o ','wo','rl','d!');
foreach($tmp AS &$a) {
  // some processing
}
unset($tmp);
?>
Run Code Online (Sandbox Code Playgroud)

我怎么能在Python v2.7中做到这一点?

我以为这样做会:

the_string = 'hello world!'
tmp = the_string.split('',2)
for a in tmp:
  # some processing
del tmp
Run Code Online (Sandbox Code Playgroud)

但它返回一个"空分隔符"错误.

有什么想法吗?

python string

1
推荐指数
1
解决办法
829
查看次数

消极的环顾四周

以下正则表达式是错误的 - 有人可以帮助找到所有:-)前面没有"请忽略我"吗?我之前不需要这样的正则表达式.单词边界可能会混淆事物.
谢谢

<script type="text/javascript">
function regexTest(string, assert) {
  document.write('<hr>')
  document.write(string)
  document.write("[")
  document.write(string.match(/\b((?!please ignore me )\:\-\))\b/gi)!=null);
  document.write("]?" + assert);
}

regexTest("1. this is the first string :-)        ",true);
regexTest("2. :-)",true)
regexTest("3. another string:-)here",true);
regexTest("4. Ending a sentence with :-)",true);
regexTest("5. please ignore me :-)",false);
</script>
Run Code Online (Sandbox Code Playgroud)

javascript regex lookbehind

1
推荐指数
1
解决办法
1158
查看次数

魔术方法上的@StaticMethod或@ClassMethod装饰

我试图将魔术方法装饰__getitem__成课堂上的类方法.这是我尝试的样本.我不介意使用classmethod或staticmethod装饰,但我不太清楚如何做到这一点.这是我尝试过的:

import ConfigParser

class Settings(object):
   _env = None
   _config = None

   def __init__(self, env='dev'):
    _env = env
    # find the file
    filePath = "C:\\temp\\app.config"

    #load the file
    _config = ConfigParser.ConfigParser()
    _config.read(filePath)

   @classmethod
   def __getitem__(cls, key): 
    return cls._config.get(cls._env, key)

   @classmethod
   def loadEnv(cls, env): 
    cls._env = env
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试调用时,Settings['database']我收到以下错误.

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: expected Array[Type], got str
Run Code Online (Sandbox Code Playgroud)

谁能告诉我我做错了什么.此外,有人可以建议是否有更好的方法来做到这一点?我甚至尝试过使用MetaClasses,但收效甚微(因为我不太了解python).

 class Meta(type):
  def __getitem__(*args):
   return type.__getitem__(*args)

 class Settings(object):
  __metaclass__ = Meta
Run Code Online (Sandbox Code Playgroud)

提前致谢.

python static-methods decorator magic-methods

1
推荐指数
1
解决办法
1527
查看次数

PHP:将变量转换为数组

我正在尝试将变量转换为数组并使用PHP拆分每个字符.

所以说我有一个变量$name = 'John Smith';如何将其转换为:

array('J','o','h','n',' ','S','m','i','t','h');
Run Code Online (Sandbox Code Playgroud)

注意John和Smith之间的空间.

谢谢.

php arrays string

1
推荐指数
2
解决办法
6476
查看次数

在Python中计算阶乘

在计算时

math.factorial(100)
Run Code Online (Sandbox Code Playgroud)

我明白了:

93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000L

为什么数字末尾有一个L?

python syntax integer python-2.x

1
推荐指数
1
解决办法
8145
查看次数

如何在python的类函数定义中调用self.value?

如何self.value在函数定义中调用 a ?

class toto :
    def __init__(self):
         self.titi = "titi"
    def printiti(self,titi=self.titi):
          print(titi)
Run Code Online (Sandbox Code Playgroud)

python class function self

1
推荐指数
1
解决办法
4380
查看次数

是否有更优雅/ pythonic的方式来表达这个结构?

itemList = ["a","b","c","d","e","f","g","h"]
aa = "NULL"
bb = "NULL"
cc = "NULL"
for item in itemList:
    aa = bb
    bb = cc
    cc = item
    if aa == "NULL":
        continue
    print "%s_%s_%s" % (aa, bb, cc)
Run Code Online (Sandbox Code Playgroud)

python list

1
推荐指数
1
解决办法
282
查看次数

按字母顺序排序if语句不起作用

下面的if语句在某处有问题,我无法弄明白.任何可能导致其无法正常运行的约定或方法滥用?checkList是用户输入的句子,lis是一个很大的单词列表.

def realCheck(checkList):  
        string = "".join(checkList)  
    print string  
    wordList = string.split()  
    if match(wordList, lis).sort(key=str.lower) == wordList.sort(key=str.lower):  
        return True  
    else:  
        return False  
Run Code Online (Sandbox Code Playgroud)

python mutators

1
推荐指数
1
解决办法
554
查看次数

替换字符

我有很多特殊字符串.我想替换字符的所有特殊字符"-" 的字符并不需要像被替换"-""A-Za-z0-9"

php regex

1
推荐指数
1
解决办法
339
查看次数