Python Learning note 6-function variables

Source: Internet
Author: User

Python Learning note 6-function variables


Local variables and global variables

Any variable in Python has a specific scope

Variables defined in a function can be used only within the function, which can only be used in specific parts of the program we call local variables

Variables defined at the top of a file can be called by any function in the file, which can be used as a variable for the entire program we call global variables

[[email protected] ~]# vim value.py#!/usr/bin/pythonx=100def Fun (): Global x x +=1 Global y Y=1fun () print Xpri NT Y


[email protected] ~]# python value.py

101

1


If you do not use Global X and do not declare the local variable as a global variable, you will get an error

#global x

[email protected] ~]# python value.py

Traceback (most recent):

File "value.py", line 8, <module>

Fun ()

File "value.py", line 5, in fun

X +=1

unboundlocalerror:local variable ' x ' referenced before assignment



This article comes from "Plum blossom fragrance from bitter cold!" "Blog, be sure to keep this provenance http://daixuan.blog.51cto.com/5426657/1790094

Python Learning note 6-function variables

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.