参数的几种用法
位置参数
即将值与形参的位置对应,完成赋值
def say():
print("says %s to %s" % ("hello", "world"))
关键字参数
def say(what, to_who):
print("says {what} to {to_who}".format(what=what, to_who=to_who))
say(to_who='world', what='hello')
收集/分配参数 (可选参数)
用
*
号 或**
号表示的形参,*
接收的元组,**
接收的是字典
def hello(*para):
# *para : It means any numbers of parameters.Additional, the type of the para is a tuple.
# *para : 表示参数para是不定数量的可选输入的,另外,它的类型是一个元组类型。
print(para) # 注意,这里不是 print(*para)
print(type(para))
def hello_2(**para):
# **para : It means collect the keyword parameters, the type is a dict.
# **para : 表示参数para是搜集关键字参数的,类型为字典。
print(para)
print(type(para))
私有参数
_
形参前使用单下划线表示保护类型,只能允许其本身与子类进行访问__
形参前使用双下划线表示私有类型,只能允许这个类本身进行访问了,连子类也不可以用于命名一个类属性(类变量) 不同于 Java,Python 中如果不允许修改生效则需要以“属性”的方式限定
class A:
# 定义类属性(类变量), 可通过类名访问,其被所有实例共享
word = 'hello'
# 以单下划线开头的表示的是protected类型的变量。即保护类型只能允许其本身与子类进行访问。
# 若内部变量标示,如: 当使用“from M import”时,不会将以一个下划线开头的对象引入。
_p1 = 'Mr'
# 双下划线的表示的是私有类型的变量。只能允许这个类本身进行访问了,连子类也不可以用于命名一个类属性(类变量)。
# 调用时名字被改变(在类FooBar内部,__boo变成_FooBar__boo,如self._FooBar__boo)
__p2 = 'Miss'
def p(self):
# 定义实例属性
print(self.word)
print(self._p1)
print(self.__p2)
def i(self):
print(self.word)
print(A._p1)
print(A.__p2)
class B(A, Exception):
pass
if __name__ == '__main__':
a = A()
a._p1 = '1'
a.__p2 = '2'
a.p()
a.word = 'hi'
a.i()
a.__p2 = "what"
b = B()
print(b._p1 + "子类访问._p1")
print(b.__p2 + "子类访问.__p2")
作用域/命名空间
具体文档 https://docs.python.org/3/tutorial/classes.html#python-scopes-and-namespaces
the innermost scope, which is searched first, contains the local names
the scopes of any enclosing functions, which are searched starting with the nearest enclosing scope, contains non-local, but also non-global names
the next-to-last scope contains the current module’s global names
the outermost scope (searched last) is the namespace containing built-in names
也就是说 作用域/命名空间 的定义与查找优先级遵循的是 “L-E-G-B”
- 作用域根据 Python 语法和缩进而明确
- 当全局变量与局部(本地)变量重名时,很容易因忽视而出现“遮盖”问题。如果需要使用全局变量,则用
global
声明 - 如果使用的是函数,即使未声明使用全局变量,也可能会“就近”引用声明 ```python def hi(a): print(a, b)
if name == ‘main‘: b = ‘world’ hi(‘hello’) # 输出结果: hello world
如果意图是使用全局参数,改进的方式:
def hi(a): print(a, globals()[‘b’])
- 嵌套函数中,内部函数的变量意图引用外部函数的变量,可以使用 `nonlocal`
```python
def say():
b = 'worlds'
def hi():
a = 'hello'
nonlocal b
print(a, b)
hi()
if __name__ == '__main__':
say() # 输出结果: hello worlds