我有以下功能示例(数字列表的平均值):
def avg(obs):
return (1. / len(obs)) * np.sum(obs)
avg([1,2,3,4,5])
3.0
Run Code Online (Sandbox Code Playgroud)
我很有兴趣了解如何使用单个可迭代参数和两个或多个参数 max()
max(1,2,3,4,5)
5
max([1,2,3,4,5])
5
Run Code Online (Sandbox Code Playgroud) 选择javadoc/phpdoc比定期评论有什么理由和情况?我知道语法差异是什么,但为什么要使用其中一个.它主要是语义还是其他原因我应该使用一个而不是另一个,它们是什么?
我真的不明白javadoc/phpdoc评论的目的.下一个代码块有什么问题?......这/**只是让某些评论在编辑器中变成不同颜色的一种方式...一些编辑不区分(香草崇高似乎不是)?
/*
* This block is wrapped in /** */ not /* */
* Some documentation goes here
* Below is copied from http://www.oracle.com/technetwork/java/javase/documentation/index-137868.html
* @param url an absolute URL giving the base location of the image
* @param name the location of the image, relative to the url argument
* @return the image at the specified URL
* @see Image
*/
Run Code Online (Sandbox Code Playgroud)
另一个问题......会有任何理由我为什么要同时使用/** */,并/* */在同一个文件?
最后的问题...为什么javadoc/phpdoc评论看起来似乎与类有关...但是我看到它们被用作对我最初理解的页面的介绍评论?
实际上又是另一个......冒着回答我自己的问题的风险我想知道javadoc/phpdoc注释真的只是区分工具自动编写的注释和开发人员编写的注释吗?
在.NET库和许多第三方库中,我可以看到有一些关于如何更改内部(私有)成员值的约定,它们支持属性(.NET属性)或方法(这些在java中常用)作为Setters和Getters)或两者兼容程序员.
我想知道何时(以及为什么)使用属性,方法或两者.例如,可以使用Visible属性(在Windows窗体中)或调用方法Show()或Hide()来更改控件的可见性.有一些值(我知道有一些内部/私有值)只能通过调用方法(如setter)来更改.在设计我自己的控件时,类我有点犹豫选择属性或方法(或支持两者?),事实上,选择其中任何一个也是可以的(Java是一个仅使用方法的例子).我知道Properties相对于方法(作为getter和setter)的优势,但如果是这样的话,为什么我们必须在某些情况下使用方法,它们会被呈现给程序员甚至替换属性.我对此感到有点困惑,对我而言(正如我所说),我只使用方法时感觉很好(就像我在java中一样,它并不是那么糟糕:).
我想知道你的经历以及你的会议.我喜欢遵循编程中的标准约定(针对每个编程环境和每种语言)来区分它们.有些人发明了自己的风格,这些风格对于其他程序员来说有点难以理解,例如在C#中使用C++命名约定(甚至PHP命名约定?)?对我来说有点奇怪,抱歉.
请分享,谢谢......
class merged(){
//i want to be able to use class sample1 and sample2 by just calling this merged class
}
class sample1(){}
class sample2(){}
Run Code Online (Sandbox Code Playgroud)
或者这样做不理想?能否请您建议我如何更有效地实施?
我打算发表一个switch声明,但后来意识到它无法对付一个string.然后我写了一个if/ else if/ else语句,然后意识到我不应该让我的功能这么久.那么我将把函数调用放在每个级联if块的主体中,并认为使用hash_map/ unordered_map到一组函数指针可能更好.
我的问题是:
使用hash_map指向要使用的函数并以这种方式调用它会更有效吗?
IIRC,地图应该接近O(1),其中作为级联,如果需要一直向下测试,直到找到匹配为O(N).但是,O(1)在什么时候(N)使用带有字符串键的映射来超越O(N)性质?
编码风格更好吗?
由于我正在减少将特定代码封装到特定于需要完成的操作的较小函数中,我认为这是真的.我知道这更像是一个意见问题,但作为一个社区,我认为这仍然是一个有效的问题.
如果我没有弄错,用C++编写类的传统方法如下(这只是一个说明性的例子):
MyClass.h
// MyClass.h
#ifndef MY_CLASS_H
#define MY_CLASS_H
class MyClass
{
public:
MyClass();
MyClass(int);
void method1(int);
int method2();
private:
int field;
};
#endif
Run Code Online (Sandbox Code Playgroud)
MyClass.cpp
// MyClass.cpp
#include "MyClass.h"
MyClass::MyClass()
{
field = 0;
}
MyClass::MyClass(int n)
{
field = n;
}
void MyClass::method1(int n)
{
field = n;
}
int MyClass::method2()
{
return field;
}
Run Code Online (Sandbox Code Playgroud)
main.cpp中
// main.cpp
#include <iostream>
#include "MyClass.h"
using namespace std;
int main()
{
MyClass mc;
mc.method1(2);
cout << mc.method2();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这个项目的传统C#等价物是什么?此外,如果我在上面的例子中错误地描绘了传统上正确的C++,请修复它以帮助防止混淆未来的读者.
在这种情况下,哪种更好/优化的方式来声明和使用变量?
int i;
for(i = 0; i < 10; i++)
Console.WriteLine(i);
for(i = 0; i < 100; i++)
Console.WriteLine(i);
Run Code Online (Sandbox Code Playgroud)
要么
for(int i = 0; i < 10; i++)
Console.WriteLine(i);
for(int i = 0; i < 100; i++)
Console.WriteLine(i);
Run Code Online (Sandbox Code Playgroud) 为了实现可迭代的独特元素,[2]是否可以接受?
# [1]
if element not in list:
list.append(element)
# [2]
dict[element] = None # value doesn't matter
Run Code Online (Sandbox Code Playgroud) 我有这样的代码:
count = 0
for line in lines:
#do something with line
#do something more with line
#finish doing that thing with line
count = count + 1
if count % 10000 == 0:
print count
Run Code Online (Sandbox Code Playgroud)
这是在python中维护count变量的正确方法吗?我可以让它看起来更好吗?
我已经阅读了关于访问器方法的3种不同约定的社区wiki查询,并且看到以下约定并不令人惊讶:
const unsigned& amount() const { return _amount; }
unsigned& amount() { return _amount; }
Run Code Online (Sandbox Code Playgroud)
是的,它与无缝的完全不同,因为它能够完全避免括号() - 这会(我觉得)是想法 - 但它仍然是某种东西; 对?