函数和变量生存区间

1
2 >>> Saysome(‘袁重阳‘,‘你好‘)
3 袁重阳->你好
4 >>> Saysome(‘你好‘,‘袁重阳‘)
5 你好->袁重阳
6 >>> Saysome(words=‘你好‘,name=‘袁重阳‘)
7 袁重阳->你好
8 >>>     #可以通过提示形参来避免编译器按照顺序赋值 . 

`

1 >>> def saysome(name=‘小甲鱼‘,words=‘让编程改变世界‘):
2     print(name+"->"+words)
3 >>> saysome()
4 小甲鱼->让编程改变世界
5 >>> saysome(‘老甲鱼‘)
6 老甲鱼->让编程改变世界
7 >>> saysome(words=‘不让编程改变世界‘)
8 小甲鱼->不让编程改变世界

收集参数.

>>> def test(*params):
    print("参数的长度是:",len(params))
    print("第二个参数是:",params[1])

>>> test(1,‘小甲鱼‘,3.14,5,6,7,8)
参数的长度是: 7
第二个参数是: 小甲鱼
>>> def test(*params,tex):
    print("参数的长度是:",len(params))
    print("第二个参数是:",params[1])

>>> test(1,2,3,4,5,6,7,8)
Traceback (most recent call last):
  File "<pyshell#38>", line 1, in <module>
    test(1,2,3,4,5,6,7,8)
TypeError: test() missing 1 required keyword-only argument: ‘tex‘
>>> #发生错误  因为调用的时候里面的参数被全部传送至   收集参数那里.
>>> test(1,2,3,4,5,6,7,tex=8)
参数的长度是: 7
第二个参数是: 2
>>> #可以通过上述的指定参数来避免 当然也可以通过默认参数避免
>>> def test(*params,tex=8):
    print("参数的长度是:",len(params))
    print("第二个参数是:",params[1])

>>> test()
参数的长度是: 0
Traceback (most recent call last):
  File "<pyshell#44>", line 1, in <module>
    test()
  File "<pyshell#43>", line 3, in test
    print("第二个参数是:",params[1])
IndexError: tuple index out of range
>>> test(1,2,3)
参数的长度是: 3
第二个参数是: 2
>>> def hello():
    print("hello")

>>> temp=hello()
hello
>>> type(temp)
<class ‘NoneType‘>
>>> print(hello)
<function hello at 0x02E32858>
>>> print(temp)
None
>>> #    可以看出没有返回值
>>> def hello():
    print("hello")
    return 1

>>> print(hello())
hello
1
>>> temp=hello()
hello
>>> print(temp)
1
>>> def hello():
    return 1,2,3,4,5,6

>>> #返回多个值
>>> hello()
(1, 2, 3, 4, 5, 6)

下面说重点函数变量的作用域问题

在函数之中声明的变量都是局部变量 , 在函数外面声名的就是全局变量 , 在函数中如果要修改全局变量的话当你赋值 , 编译器会再在函数之中声明一个局部变量 而且 当你声明的时候 全局变量会被隐藏 . 如果小伙子 实在想在函数内部修改全局变量的话 . 可以在函数中 将该全局变量进行一次全局变量定义然后开始修改 . 修改代码如下

 1 def dis(price,rate):
 2     final_price=price*rate
 3     global old_price
 4     old_price=10
 5     print(old_price)
 6     return final_price
 7 old_price=float(input(‘请输入原价:\t‘))
 8 rate=float(input(‘请输入折扣率:\t‘))
 9 new_price=dis(old_price,rate)
10 print(‘打折后的价格是:\t‘,new_price)
11 print("这里试图输出修改后的全局变量old_price:\t",old_price)

实验代码如下

def dis(price,rate):
    final_price=price*rate
    #print("这里试图打印全局变量old_price的值:\t",old_price)
    # old_price=88       #这里试图修改    全局变量
    #print(‘修改后old_price的值是:\t‘,old_price)
    #old_price=80  #如果在这里试图修改全局变量 old_price的话编译器会再声明一个局部变量.
    print(old_price)
    return final_price
old_price=float(input(‘请输入原价:\t‘))
rate=float(input(‘请输入折扣率:\t‘))
new_price=dis(old_price,rate)
#print(‘修改后的old_price的值是:\t‘,old_price)
print(‘打折后的价格是:\t‘,new_price)
#在函数之中定义的变量都是局部变量在变量外无效 .
#因为在执行函数的时候我们将其储存带 "栈" 中当函数执行完毕的时候我们将该函数从
# "栈" 中删除 所以这时候其中的变量也被随之删除了 .在外界不能被访问 .
#print(‘这里试图打印局部变量final_price的值:\t‘,final_price)    # 这一句话有错 , 局部变量在外面不能访问 .

# 在和局部变量相对的是全局变量  全局变量是在 函数之外声明的 (其作用于参考局部变量可以得出 : 是整个文件 . 因为整个文件自始至终都存在 . )
时间: 2024-08-04 13:56:58

函数和变量生存区间的相关文章

2016-05-30 函数与变量 作用域

1. 如何访问函数内的局部变量 <script> //获取函数内部变量的两种方法 function fn1(){ var a='100000美金'; } alert(a);//报错 a isNot undefine; //解决方法1: 通过把局部变量赋值给全局变量 var str=''; function fn1(){ var a='100000美金'; str=a; } fn1();//必须先调用函数,否则里面赋值不会自动执行 alert(str);//'100000美金' //解决方式2

函数、变量

静态与动态 HML  静态(数据静态) ASP/ASP.NET动态(数据动态) C#编程: (一)项目结构 .cs——源文件(程序代码) .csproj——项目文件(管理文件项) .sln——解决方案文件(管理项目) .config——配置文件 函数的四要素:名称,输入,输出,加工 主函数 static void Main(string[] args) { } 输入语句 string s = Console.ReadLine(); 输出语句 Consle Writeline(“要输出的内容”)

VC和gcc在保证函数static变量线程安全性上的区别

VC和gcc不同,不能保证静态变量的线程安全性.这就给我们的程序带来了很大的安全隐患和诸多不便.这一点应该引起我们的重视!尤其是在构造函数耗时比较长的时候,很可能给程序带来意想不到的结果.本文从测试代码开始,逐步分析原理,最后给出解决方案. 多线程状态下,VC不能保证在使用函数的静态变量的时候,它的构造函数已经被执行完毕,下面是一段测试代码: class TestStatic { public: TestStatic() { Sleep(1000*10); m_num = 999; } publ

10.C#匿名函数的变量捕获(五章5.5)

小分享:我有几张阿里云优惠券,用券购买或者升级阿里云相应产品最多可以优惠五折!领券地址:https://promotion.aliyun.com/ntms/act/ambassador/sharetouser.html?userCode=ohmepe03 首先感谢园友的指定,后续的文章一定会多码多想,出来的文章才有说服力.那今天接上篇我们来聊一聊匿名函数,对于匿名函数,我们知道使用delegate关键字,那我们来需要知道匿名函数在变量是的处理方式,先说两个术语,外部变量和捕获的外部变量,可以看出

js 函数和变量的提升

1. 函数的作用域: js中 ,函数的作用域为函数,而不是大括号. var hei = 123;if(true){ hei = 456;}console.log(hei);// 456; var hei = 123;if(true){ (function(){ var hei = 456;})(); }console.log(hei); // 123 函数内部可以用用函数外部的变量,而函数外部的不可以用函数内部的变量(可以用闭包实现效果,随后总结). (function(){ var hei =

辨析函数指针变量和指针型函数

在上一篇随笔(顺序表基本操作算法的代码实现)中,LocateElem()函数的第三个形参的形式是: Status (*compare)(Elemtype e,Elemtype temp); 这是一个函数指针变量,借此机会记录一下函数指针变量和指针型函数的区别. 一.写法上的区别 函数指针变量 指针型函数 int (*function)(int i); int  *function(int i){} 上面是一个例子,可看到函数指针变量只是在:*function处比指针型函数多了一对小括号,下面是两

【C语言】用函数指针变量完成:输入两个整数,让用户选择函数,选择1输出较大的数,选择2输出较小的数

<pre name="code" class="cpp">//用函数指针变量完成:输入两个整数,让用户选择函数,选择1输出较大的数,选择2输出较小的数 #include <stdio.h> int max(int x,int y) { return (x>y)?x:y; } int min(int x,int y) { return (x>y)?y:x; } int main() { int (*p)(int,int); int

MIC中函数和变量的声明

c++/c使用 __declspec(target(mic))函数或变量声明 或 __attribute__((target(mic)))函数或变量声明 举例如下: __attribute__((target(mic))) int a; __attribute__((target(mic))) void func(); 这里注意attribute前后均是两个下划线,示例代码如下: #include<stdlib.h> #include<stdio.h> #include<st

【C/C++学院】0819-/类的成员函数与const-mutable /构造与析构/拷贝构造deletedefault以及深浅拷贝/静态成员函数成员变量类在内存的存储默认参数/友元类以及友元函数

类的成员函数与const-mutable 成员函数 Fushu.h #pragma once #include <iostream> class fushu { public: int x; int y; public: fushu(); ~fushu(); void show(); inline void showall(int x, int y);//显式内联 void setxy(int x, int y);//编译器优化,默认隐式内联 void show(int x, int y);