我有一个名为的类Note
,其中包含一个名为的实例变量time_spent
.我希望能够做到这样的事情:
current_user.notes.inject{|total_time_spent,note| total_time_spent + note.time_spent}
Run Code Online (Sandbox Code Playgroud)
这可以通过在Enumerable模块中混合来实现吗?我知道你应该添加include Enumerable
到类中然后定义一个each
方法,但是每个方法应该是一个类还是实例方法?是什么在变each
的方法?
我正在使用Ruby 1.9.2
例如,编译器是否知道要翻译
string s = "test " + "this " + "function";
Run Code Online (Sandbox Code Playgroud)
至
string s = "test this function";
Run Code Online (Sandbox Code Playgroud)
从而避免字符串连接的性能损失?
我正试图找到"打破非公开方法"的解决方案.
我只是想调用RuntimeMethodInfo.InternalGetCurrentMethod(...)
,传递我自己的参数(所以我可以实现GetCallingMethod()
),或直接RuntimeMethodInfo.InternatGetCurrentMethod(ref StackCrawlMark.LookForMyCaller)
在我的日志记录例程中使用.GetCurrentMethod
实施方式如下:
[MethodImpl(MethodImplOptions.NoInlining)]
public static MethodBase GetCurrentMethod()
{
StackCrawlMark lookForMyCaller = StackCrawlMark.LookForMyCaller;
return RuntimeMethodInfo.InternalGetCurrentMethod(ref lookForMyCaller);
}
Run Code Online (Sandbox Code Playgroud)
InternalGetCurrentMethod
声明在哪里:内部:-).
我使用反射调用方法没有问题,但这会弄乱调用堆栈,这只是必须要保留的一件事,否则它会破坏它的目的.
我的堆栈跟踪保持接近原始的几率是多少(至少在允许的StackCrawlMark
s 的距离内,这是LookForMe
,LookForMyCaller
和LookForMyCallersCaller
.是否有一些复杂的方法来实现我想要的?
我最近写了一个相当丑陋的单行,并且想知道将它分成多行是否更好的python风格,或者留下它作为评论的单行.我查看了PEP 8,但它没有提到这一点
这是我写的代码:
def getlink(url):
return(urllib.urlopen(url).readlines()[425].split('"')[7])
# Fetch the page at "url", read the 426th line, split it along
# quotes, and return the 8th quote delimited section
Run Code Online (Sandbox Code Playgroud)
但这样的事情会更好吗?:
def getlink(url):
url_file = urllib.urlopen(url)
url_data = url_file.readlines()
line = url_data[425]
line = line.split('"')
return line[7]
Run Code Online (Sandbox Code Playgroud)
或者介于两者之间?
>>> import math
>>> math.sin(68)
-0.897927680689
Run Code Online (Sandbox Code Playgroud)
但
sin(68) = 0.927 (3 decimal places)
Run Code Online (Sandbox Code Playgroud)
关于为什么我得到这个结果的任何想法?
谢谢.
>>>
Enter muzzle velocity (m/2): 60
Enter angle (degrees): 45
Traceback (most recent call last):
File "F:/Python31/Lib/idlelib/test", line 9, in <module>
range()
File "F:/Python31/Lib/idlelib/test", line 7, in range
Distance = float(decimal((2*(x*x))((decimal(math.zsin(y)))*(decimal(math.acos(y)))))/2)
TypeError: can't multiply sequence by non-int of type 'str'
Run Code Online (Sandbox Code Playgroud)
我只是新手,所以如果真的很明显,不要太苛刻,但为什么我会收到这个错误?
我有一个类,我希望__hash__()
为这个类编写一个方法.我想写的方法将在某些情况下返回对象的默认哈希值,并在其他一些情况下返回其属性之一的哈希值.所以,作为一个简单的测试案例:
class Foo:
def __init__(self, bar):
self.bar = bar
def __hash__(self):
if self.bar < 10:
return hash(self) # <- this line doesn't work
else:
return hash(self.bar)
Run Code Online (Sandbox Code Playgroud)
这个问题就是hash(self)
简单地调用self.__hash__()
,导致无限递归.
我收集到一个对象的哈希是基于该对象的哈希id()
,所以我可以重写return hash(self)
为return id(self)
,或者return id(self) / 16
,但是在我自己的代码中重新创建默认实现似乎是不好的形式.
我也想到我可以把它重写为return object.__hash__(self)
.这可行,但似乎更糟糕,因为不打算直接调用特殊方法.
所以,我要问的是; 有没有办法使用对象的默认哈希而不隐式调用该__hash__()
对象是该实例的类的方法?
我试图压缩一些具有类似结构的重复代码:
match self.foo() {
None => self.bar(),
Some(MyStruct { foo: x, .. }) => match x {
Pattern1 => result,
Pattern2 => {
block_result
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想写的类似于:
my_macro!(
Pattern1 => result,
Pattern2 => {
block_result
}
)
Run Code Online (Sandbox Code Playgroud)
避免重复None
处理和MyStruct
解构.
这看起来应该很简单,因为它基本上只是将宏体代入匹配表达式,但我实际上看不到任何方法可以做到这一点.
我尝试按如下方式编写宏:
macro_rules! my_macro (
($($pat:pat => $result:expr,)*) => (
match self.foo() {
None => self.bar(),
Some(MyStruct { foo: x, .. }) => match x {
$(
$pat => $result,
)*
},
}
);
)
Run Code Online (Sandbox Code Playgroud)
但这失败了,因为匹配臂的RHS可以是表达式或块(并且它也没有处理可选地省略最后一个臂的逗号).据我所知,没有办法指定宏模式的一部分可以是块或表达式,所以我想不出解决这个问题的方法.
理想情况下,我想这样做而不必编写复杂的模式来解构匹配的主体只是为了再次将它们重新组合在一起,但我认为没有任何指示符可以接受匹配表达式的主体. …
python ×5
c# ×2
methods ×2
.net ×1
coding-style ×1
count ×1
hash ×1
integer ×1
macros ×1
math ×1
optimization ×1
performance ×1
private ×1
reflection ×1
ruby ×1
rust ×1
trigonometry ×1