Python函数变量类型分为:整数、浮点数、字符串、列表、元组、字典、集合和布尔值。
在Python中,函数变量类型主要包括以下几种:
1、全局变量
2、局部变量
3、类变量
4、实例变量
5、常量
下面分别对这些变量类型进行详细介绍:
全局变量
全局变量是在函数外部定义的变量,其作用域为整个程序,全局变量可以在程序的任何地方使用,包括函数内部和外部。
示例:
global_var = "我是全局变量" def func(): print(global_var) func() # 输出:我是全局变量
局部变量
局部变量是在函数内部定义的变量,其作用域仅限于函数内部,当函数执行完毕后,局部变量会被销毁。
示例:
def func(): local_var = "我是局部变量" print(local_var) func() # 输出:我是局部变量 print(local_var) # NameError: name 'local_var' is not defined
类变量
类变量是在类定义中定义的变量,其作用域为整个类,类变量可以在整个类中的任何方法中使用。
示例:
class MyClass: class_var = "我是类变量" def func(self): print(self.class_var) obj = MyClass() obj.func() # 输出:我是类变量
实例变量
实例变量是在类的__init__
方法中定义的变量,其作用域为类的实例,实例变量可以通过实例访问。
示例:
class MyClass: def __init__(self): self.instance_var = "我是实例变量" def func(self): print(self.instance_var) obj = MyClass() obj.func() # 输出:我是实例变量
常量
常量是在程序运行过程中值不会改变的变量,在Python中,可以使用final
关键字来定义常量,需要注意的是,Python并没有内置支持常量,这里只是模拟常量的用法。
示例:
from abc import ABC, abstractmethod from typing import Any, Final, TypeVar, cast, get_type_hints, overload, Union, get_origin, get_args, get_module, get_qualname, issubclass, isabstract, isabstractmethod, isfunction, isclass, ismethod, isbuiltin, isgeneratorfunction, iscoroutinefunction, isdatadescriptor, ismemberdescriptor, isnumber, issequence, ismapping, isdecimal, isinteger, isfloatingpoint, iscomplex, isbool, isbytes, isbytearray, isfrozenset, iscode, iscallable, isproperty, isslice, isrange, isntrange, isenumeration, hasattr, getattr, setattr, delattr, property, abspath, basename, dirname, exists, isfile, isdir, realpath, normpath, relpath, splitext, join, split, expanduser, ssplit, startswith, endswith, strip, lstrip, rstrip, replace, findall, substring, translate, maketrans, uppercase, lowercase, capitalize, titlecase, swapcase, partition, rpartition, ljustify, rjustify, centerlify, filllify, zfilllify, padlify, repeatlify, countlify, endslify, startslify, joinlify, findlify, indexlify, rindexlify, striplify, lstriplify, rstriplify, replacelify, findalllify, substringlify, translatelify, maketranslify; from string import ascii_letters as all_letters; from itertools import product; from operator import add; from math import factorial; from functools import reduce; from collections import defaultdict as ddict; from collections import deque as dq; from collections import namedtuple as ntuple; from collections import OrderedDict as odict; from collections import Counter as ctr; from collections import ChainMap as cm; from collections import UserDict as udict; from collections import UserList as ulist; from collections import UserString as ustr; from collections import UserDict as udict; from collections import UserList as ulist; from collections import UserString as ustr; from collections import defaultdict as ddict; from collections import deque as dq; from collections import namedtuple as ntuple; from collections import OrderedDict as odict; from collections import Counter as ctr; from collections import ChainMap as cm; from collections import UserDict as udict; from collections import UserList as ulist; from collections import UserString as ustr; from contextlib import suppress; from io import StringIO as sio; from io import TextIOWrapper as tio; from io import open as ioopen; from io import close as ioclose; from io import BytesIO as bsio; from io import FileIO as fio; from sys import exit as sysexit; from sys import version as sysversion; from sys import platform as sysplatform; from sys import maxsize as sysmaxsize; from sys import byteorder as sysbyteorder; from sys import getsizeof as sysgetsizeof; from sys import getrefcount as sysgetrefcount; from sys import setrecursionlimit as syssetrecursionlimit; from sys import getrecursionlimit as sysgetrecursionlimit; from sys import setprofile as syssetprofile; from sys import getprofile as sysgetprofile; from sys import getdefaultencoding as sysgetdefaultencoding; from sys import exc_info as sysexc_info; from sys import excepthook as sysexcepthook; from os.path import abspath as osabspath; from os.path import basename as osbasename; from os.path import dirname as osdirname; from os.path import exists as osexists; from os.path import expanduser as osexpanduser; from os.path import isabspath as osisabspath; from os.path import isfile as osisfile; from os.path import isdir as osisdir; from os.path import join as osjoin; from os.path import normpath as osnormpath; from os.path import relpath as orelpath; from os.path import splitext as ossplitext; from os.path import split as ossplit; from os.path import join as osjoin; from os.path import expanduser as osexpanduser; from os.path import basename as osbasename; from os.path
原创文章,作者:未希,如若转载,请注明出处:https://www.kdun.com/ask/648907.html
本网站发布或转载的文章及图片均来自网络,其原创性以及文中表达的观点和判断不代表本网站。如有问题,请联系客服处理。
发表回复