From 5e8d8e073bf0e208927c20103d3fc383f9e90316 Mon Sep 17 00:00:00 2001 From: qiaolongfei Date: Tue, 27 Jun 2017 23:53:37 +0800 Subject: [PATCH] refine GetOrCreateVariable --- paddle/framework/scope.cc | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/paddle/framework/scope.cc b/paddle/framework/scope.cc index ed75aece0..31956aecb 100644 --- a/paddle/framework/scope.cc +++ b/paddle/framework/scope.cc @@ -34,15 +34,17 @@ Variable* Scope::GetVariable(const std::string &name) const { } Variable* Scope::GetOrCreateVariable(const std::string &name) { - Variable* var; - var = GetVariable(name); - if (var == nullptr) { - auto err = CreateVariable(name); - if (!err.isOK()) { - return nullptr; - } + Variable* var = GetVariable(name); + if (var != nullptr) { + return var; + } + + Error err = CreateVariable(name); + if (!err.isOK()) { + return nullptr; + } else { + return GetVariable(name); } - return GetVariable(name); } bool Scope::HaveVariable(const std::string &name) { -- GitLab