print.__doc__ 输出:
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)
在哪里
>>> getattr(__builtin__,"print").__doc__
Run Code Online (Sandbox Code Playgroud)
输出:
Run Code Online (Sandbox Code Playgroud)print(value, ..., sep=' ', end='\n', file=sys.stdout)将值打印到流,或
sys.stdout默认情况下打印.可选关键字参数:file:类文件对象(stream); 默认为当前
sys.stdout.
sep:在值之间插入的字符串,默认为空格.
end:在最后一个值后附加的字符串,默认为换行符.
任何人都可以帮助我理解为什么print.__doc__给出语法错误而不是打印文档字符串
在最后两行以下程序,static_cast<void*>并dynamic_cast<void *>表现不同.据我所知,的结果dynamic_cast<void*>总是解析为完整的对象的地址.所以它以某种方式使用RTTI.谁能解释一下编译器如何使用RTTI两者之间的区分.
#include <iostream>
using namespace std;
class Top {
protected:
int x;
public:
Top(int n) { x = n; }
virtual ~Top() {}
friend ostream& operator<<(ostream& os, const Top& t) {
return os << t.x;
}
};
class Left : virtual public Top {
protected:
int y;
public:
Left(int m, int n) : Top(m) { y = n; }
};
class Right : virtual public Top {
protected:
int z;
public:
Right(int m, …Run Code Online (Sandbox Code Playgroud) class A():
def tmp(self):
print("hi")
def b(a):
a.tmp()
Run Code Online (Sandbox Code Playgroud)
要检查是否在b中调用了tmp方法,推荐的方法是
a = A()
a.tmp = MagicMock()
b(a)
a.tmp.assert_called()
Run Code Online (Sandbox Code Playgroud)
但是这里的tmp被嘲笑了,并没有导致"hi"被打印出来。
我希望我的单元测试可以检查方法tmp是否被调用而不会模拟它。
这可能吗?
我知道在编写unitest时这不是标准的事情。但是我的用例(有点棘手)需要这样做。
我一直在阅读有关如何分析我的火花簇的信息。注意:我正在使用 pyspark。
我已经能够集成 cProfiler 以获取驱动程序级别和每个 RDD 级别的时间指标。但 cProfile 只能帮助节省时间。
如何分析我的 Spark 应用程序(使用 py-spark 编写)的内存使用情况?
我有兴趣找到内存和时间瓶颈,以便我可以重新访问/重构该代码。
另外,有时当我将更改推送到生产时,会导致 OOM(在执行器处),并且我最终会被动地修复代码。我认为将它与一些内存分析器集成将帮助我在测试过程中检测到问题。
std::shared_ptr<std::string> test() {
return std::make_shared<std::string>("sdsd");
}
Run Code Online (Sandbox Code Playgroud)
cout << *test() << endl;
Run Code Online (Sandbox Code Playgroud)
上面的代码有效。有人可以告诉我“sdsd”字符串存储在哪里吗?我原以为它会出错,因为右值是一个临时对象。右值复制并存储在哪里?
使用weak_ptr
std::weak_ptr<std::string> test() {
return std::make_shared<std::string>("sdsd");
}
Run Code Online (Sandbox Code Playgroud)
cout << *test().lock() << endl;
Run Code Online (Sandbox Code Playgroud)
有趣的是上面的代码出错了。有什么不同?
我是多线程的新手.在使用条件变量在C++ 11中编写多线程代码时,我使用以下构造
while(predicate) {
cond_var.wait(&lock);
}
Run Code Online (Sandbox Code Playgroud)
但是,我一直在阅读Deitel关于操作系统的第三版书(第6页),其中使用了以下结构
if(predicate) {
cond_var.wait(&lock);
}
Run Code Online (Sandbox Code Playgroud)
那么,有什么区别?为什么这本书不能用呢?是不是虚假的称呼问题?
有m个车站和n个房屋,每个车站和房屋的(x,y)坐标给出,每个房屋输出最近的车站.
后来,这个问题被推广到从每个房子找到最近的车站.
我的看法:对于每个房子,建立一堆距离(自下而上)到车站,然后弹出k.对所有房屋都这样做.为O(n*(M + klogm));
或者,对于每个房屋,为工作站构建订单统计树,然后查找具有等级的节点并遍历该节点下方的整个树.对所有房屋都这样做.为O(n*(mlogm + 10gm的+ k))的
还有更好的替代品吗?任何基于图DS的解决方案,哪个比这更好?
我正在尝试编写一个异常安全的通用堆栈.这就是我到目前为止所做的.
#include <iostream>
#include <memory>
#include <exception>
class stk_exception:public exception
{
virtual const char* what() const throw()
{
return "stack underflow";
}
} stk_ex;
template <class T>
struct node
{
T data;
node<T> *next;
};
template <class T>
class stack_generic
{
public:
stack_generic() : _head(nullptr) {
}
void push(T x) {
node<T> *temp(new node<T>());
temp->data = x;
temp->next = _head;
_head = temp;
}
void pop() {
if (_head == nullptr) {
throw stk_ex;
} else {
node<T> *temp = …Run Code Online (Sandbox Code Playgroud) 在下面的代码中,我使用auto来定义一个与使用的迭代器兼容的新变量"temp".我只是好奇我是否可以不使用auto关键字做同样的事情?
换句话说,我不知道容器是什么.那么如何在不损害代码通用性的情况下定义临时变量呢?
作为C++的新手,我一直在使用SO作为我的网站来解决任何问题.我找了类似的问题,但我没有找到.任何帮助,将不胜感激!!
/* InsertionSort.c */
#include<iostream>
#include <typeinfo>
namespace sort {
template <class RandomAccessIterator, class Compare >
void InsertionSort(RandomAccessIterator begin, RandomAccessIterator end, Compare comp) {
RandomAccessIterator itrOuter,itrInner,itrShift;
for(itrOuter = begin+1; itrOuter!=(end); itrOuter++) {
// ***************HERE***********************************************
auto temp= *itrOuter;
for(itrInner=begin;itrInner<itrOuter;itrInner++) {
if(!comp(*itrInner,*itrOuter)) {
for(itrShift=itrOuter;itrShift> itrInner;itrShift--)
*itrShift=*(itrShift -1);
*itrInner=temp;
break;
}
}
}
}
}
bool Compa(int& x, int& y) {return (x<y);}
int main(void) {
using namespace sort;
int arr[] = {3,5,1,7,2,6,9,12};
InsertionSort(arr, arr+8, Compa );
for(int i=0;i < 8;i++)
std::cout<<arr[i]<<" …Run Code Online (Sandbox Code Playgroud) 这会发出大约100行错误.错误:使用run_me复制构造函数的已删除函数等.那么,这里的问题是什么?
#include<thread>
#include<iostream>
#include<vector>
#include<fstream>
#include<string>
#include<chrono>
using namespace std;
//creating thread to print line from file
class run_me {
ifstream fs;
public:
string s;
run_me(): fs("lliftOff.cpp",ifstream::in) {}
void operator()(){
cout<<"hi";
while(getline(fs,s) ) {
cout<<s<<endl;
this_thread::sleep_for (chrono::seconds(1));
}
}
~run_me() {
fs.close();
}
};
int main() {
thread t1{run_me()};
t1.join();
cout<<"shutting down now"<<endl;
}
Run Code Online (Sandbox Code Playgroud) 我最近阅读了有关 RAII 的内容并开始使用它。我试图将图定义为邻接列表并使用unique_ptr. 我知道我可以将它们定义为堆栈对象,但我正在尝试使用unique_ptr's 来习惯它们。
我正在做以下事情
unique_ptr to vector --
|-->(unique_ptr)list<edge>
|-->(unique_ptr)list<edge>
|-->(unique_ptr)list<edge>
|-->(unique_ptr)list<edge>
Run Code Online (Sandbox Code Playgroud)
代码:
#include<memory>
#include<vector>
#include<list>
using namespace std;
struct edge {
int vertex;
int weight;
};
int vertices = 10;
unique_ptr<vector<unique_ptr<list<struct edge > > > >
adj_list(new vector<list<struct edge> >(10)); // THIS COMPILES
unique_ptr<vector<unique_ptr<list<struct edge > > > >
adj_list(new vector<list<struct edge> >(10, new list<edge>())); //THIS GIVES COMPILATION ERROR
Run Code Online (Sandbox Code Playgroud)
谁能帮我纠正这个问题?
编辑:
我对向量是 RAII 类有疑问。如果我执行以下操作
vector<int> *v;
v = new vector<int> (10);
Run Code Online (Sandbox Code Playgroud)
我必须删除变量吗v …
c++ ×7
c++11 ×3
python ×3
algorithm ×1
apache-spark ×1
auto ×1
casting ×1
concurrency ×1
dynamic-cast ×1
exception ×1
generics ×1
magicmock ×1
mocking ×1
pyspark ×1
python-2.x ×1
python-3.x ×1
shared-ptr ×1
templates ×1
unique-ptr ×1
unit-testing ×1