should the functions be defined before it is used? but why the following code works:

def main():

dog()

def dog():

print("This is a dog.")

if __name__ == '__main__':

main()

I mean the dog() is defined after it is called, how this works?

解决方案

Code within the blocks of functions/methods does not execute before the functions are called. And the CPython language implementation (that's the most common one and the one you are probably using) does not do name-checking at compile time like languages such as C. Since it is primarily an interpreted language, it checks if names are available in the namespace dynamically at runtime.

Furthermore something you should know about Python is that code within classes is executed at the time of class creation, but code with in methods/functions is not. A def statement simply adds the name of the function to the namespace. It does not run any code within the function itself until the function is called.

Consider the following legal Python code.

class Something:

def __init__(self):

self.puppy = Dog()

class Dog:

def __init__(self):

pass

inst = Something()

This is perfectly legal because __init__ is not run before Dog class gets defined. But what if we moved that constructor above the creation of class Dog?

This code would complain about a NameError because Dog isn't defined yet!

class Something:

def __init__(self):

self.puppy = Dog()

inst = Something()

class Dog:

def __init__(self):

pass

One final example...

This code would complain about the same NameError.

class Something:

my_puppy = Dog()

def __init__(self):

self.puppy = Dog()

class Dog:

def __init__(self):

pass

inst = Something()

This is because all of the code inside Something is immediately executed and at that point in the execution, the namespace/scope does not have access to the name Dog.

python语言中函数在调用前必须先定义吗_应该在python中使用函数之前进行定义?...相关推荐

  1. python语言提供的3个基本数字类型是什么_计算机二级python学习教程(3) python语言基本数据类型...

    本文继续计算机二级python教程的学习,之前已经学习过了计算机二级python学习教程(1) .计算机二级python学习教程(2) 3.1 数字类型 数字类型:整数类型.浮点数类型.复数类型. 整 ...

  2. C语言一定要有函数声明吗,1 什么是C语言的隐式函数声明在C语言中,函数在调用前不一定非要声明。如果没有声明,那么编译器会自动按照一种隐式声明的规则,为调用函数的C代码产生汇编代码。下...

    1 什么是C语言的隐式函数声明 在C语言中,函数在调用前不一定非要声明.如果没有声明,那么编译器会自动按照一种隐式声明的规则,为调用函数的C代码产生汇编代码.下面是一个例子: int main(int ...

  3. python语言中函数在调用前必须先定义吗_Python函数必须先定义,后调用说明(函数调用函数例外)...

    java开发者在定义类中的方法时,不会关心方法的定义相对于调用语句的位置. 但是python中需要注意: 函数必须先定义.后调用(函数调用函数例外). 如下为示例说明: 1.python函数的应用一般 ...

  4. python语言中、用来表示赋值的符号是_第二章Python语言基础知识

    第二章Python语言基础知识 2.1 Python语言基础知识 2.1.1标识符与关键字 在Python语言中,对程序中各个元素命名加以区分,这种用来标识变量.函数.类等元素的符号称为标识符. Py ...

  5. python中内置的四种数值类型为_浅谈python语言四种数值类型

    Python语言支持四种不同的数值类型,包括int(整数)long(长整数)float(浮点实际值)complex (复数),本文章向码农介绍python 四种数值类型,需要的朋友可以参考一下.希望对 ...

  6. python中matplotlib条形图数值大的在最底层显示_如何使用python的matplotlib模块绘制水平条形图...

    python是一个很有趣的语言,可以在命令行窗口运行.python中有很多功能强大的模块,这篇经验告诉你,如何利用python的matplotlib模块,绘制水平条形图. 工具/原料 windows系 ...

  7. python语言的三个主要特点_python干货|新总结的4个python语言的特点,这几个细节值得关注...

    [摘要]在这个科学技术高速发展的时代,越来越多的人都开始选择学习编程软件,那么首先被大家选择的编程软件就是python,也用在各行各业之中,并被大家所熟知,所以也有越来越多的python学习者关注py ...

  8. 《Python语言程序设计》王恺 王志 机械工业出版社 第一章 初识Python 课后习题答案

    1.6 课后习题 (1)高级语言翻译为机器语言的方式有两种:一种是编译,一种是解释.Python属于解释型语言 (2)Python程序支持两种运行方式:交互式和脚本式 (3)Python的单行注释以 ...

  9. python有一分数序列求前20项和答案_求分数序列前20项和matlab

    有一个分数序列1/2,2/3,3/5,5/8,8/13,13/21.求这个序列的前20项之和.C语言编程 #includeintmain(void){inta=1,b=2,i;doublesum=0; ...

  10. 以下哪一个不属于python语言的特点-【单选题】下面的4个特点,Python不具备的是?...

    问题:[单选题]下面的4个特点,Python不具备的是? 更多相关问题 提出"学习风格"概念的学者是(). A.奥苏伯尔B.加涅C.布鲁纳D.赛伦 "春蚕到死丝方尽,蜡炬 ...

最新文章

  1. 前端学习(3173):react-hello-react之todoList教程
  2. 六十万的成长_我的EA策略分析和实现
  3. Moodle: 如何修改moodle——theme主题皮肤
  4. 忠言多少有些逆耳,创业的九条真经
  5. [HAOI2015]树上染色
  6. 小程序抽象组件使用示例
  7. pytorch之深度学习
  8. 图片的压缩(上传图片太大的话,上传不到服务器)
  9. Flume系列一之架构介绍和安装
  10. 平面设计中的网格系统pdf_小白新手想要系统学习平面设计要看哪些书?
  11. iOS安装包瘦身小记
  12. Java编一个收银小票_Java编程打印购物小票实现代码
  13. PS和AI矢量电子签名制作(详细)
  14. 图片上传返回淘宝链接
  15. 共码未来 | 盘点 2022 Google 谷歌开发者大会多重亮点!
  16. 以数据为中心的云计算——阿里云OS浅谈
  17. ubuntu 16.04安装QQ 8.X
  18. 经验分享:《节奏大师》UI优化历程
  19. window10新版本登录无限注销问题解决
  20. fragment 淡入淡出_Android ViewPager随着不同的动画,如放大,淡入淡出等

热门文章

  1. mysql backup restore_mysql-backup-restore
  2. bootstrap切换tab页局部刷新_bootstrap在 刷新页面,tab选择页面不会改变。
  3. java记秒,Java程序获取自Java时代开始以来的秒数
  4. java基类和派生类圆_java – 当基类和派生类都具有相同名称的变量时会发生什么...
  5. 最简单的图文教程,几步完成Git的公私钥配置
  6. LFYZOJ 104 Counting Swaps
  7. Redis在CentOS 7上的安装部署
  8. Python--模块微谈
  9. 记一次 nginx 504 Gateway Time-out
  10. 利用stringBuilder操作html文件