python常用语法

@property是一个装饰器,它能够使得类把一个方法变成属性调用的。

class Test:
	def __init__(self):
		print("I'm doing the test!")
	score=60
	def getScore(self):
		return self.score
	@property
	def GetScore(self):
		print(self.score)
if __name__ == '__main__':
	T=Test()
	print(T.getScore())
	T.GetScore

 

 

 

->常常出现在python函数定义的函数名后面,为函数添加元数据,描述函数的返回类型,从而方便开发人员使用。比如:

def add(x, y) -> int:
  return x+y

 这里面,元数据表明了函数的返回值为int类型。

@property
def attrs(self) -> _Attrs:
    pass

- > _Attr则表明函数返回的是一个外部可访问的类的私有变量。

参考:给函数增加元信息:

def add(x:int, y:int) -> int:
    return x + y

 函数注解只存储在函数的 __annotations__ 属性中。例如:

class Test:
	def __init__(self):
		print("I'm doing the test!")
	score=60
	def getScore(self):
		return self.score
	@property
	def GetScore(self):
		print(self.score)
	def Add(self,a:int,b:int):
		return a+b;
if __name__ == '__main__':
	T=Test()
	print(T.getScore())
	T.GetScore
	print(T.Add.__annotations__)

 

 

 

class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:

 -> List[int] 表示该函数应返回一个整数列表。

nums: List[int], target: int表示应该nums是整数列表,并且应该target是整数。

python多个返回值

使用

return one,two只返回一个元组

 

python的几种变量——按作用域分
1、全局变量:在模块内、在所有函数外面、在class外面,这就是全局变量。
2、局部变量:在函数内、在class的方法内(未加self修饰的) ,这就是局部变量
3、静态变量(也可以说,类属性):在class内的,但不在class的方法内的,这就是静态变量
4、实例变量(也可以说,实例属性):在class的方法内的,用self修饰的变量,这就是实例变量,该变量要在函数的参数内声明,//可以不必声明

参考:https://www.cnblogs.com/vincent-sh/p/12780716.html

 

 python整除

posted @ 2022-01-22 22:01  磐正  阅读(69)  评论(1)    收藏  举报