一流公民是指能够为促进其他同伴实体的所有运营提供支持的实体。
这些实体经常使用:在传递参数时,从函数中返回值,条件修改和值赋值。
在本文中,我们将研究Python 3.x或更早版本中一流公民的实现和使用情况。此外,我们还将学习所有实体获得的成为头等公民的标签。
这些公民包括变量和函数。
首先让我们熟悉一流公民的数据类型整数
浮动型
复数
弦乐
上面提到的所有四种类型在Python 3.x中都被赋予了一流公民的标签。或更早。
示例#Declaration of an integer
print("hello world")
int_inp=int(input())
print("This is a First class Citizen of "+str(type(int_inp)))
#Declaration of floating type
float_inp=float(input())
print("This is a First class Citizen of "+str(type(float_inp)))
#Declaration of complex numbers
complex_inp=complex(input())
print("This is a First class Citizen of "+str(type(complex_inp)))
#Declaration of Strings
str_inp=input()
print("This is a First class Citizen of "+str(type(str_inp)))
输入值2
23.4
4+7j
nhooo
输出结果This is a First class Citizen of
This is a First class Citizen of
This is a First class Citizen of
This is a First class Citizen of
现在让我们看一些被称为头等公民的功能
头等对象使用Python语言统一处理。面向对象的每个实体均指一个默认对象,可以在任何时间点对其进行引用和取消引用。可以使用数据结构或控制结构来完成存储。
现在我们来看看python是否支持一流的功能。因此,当任何语言将函数视为第一类对象时,都可以说支持第一类函数。
示例# Python program
# functions being be treated as objects
def comp_name(text):
return text.isupper()
print(comp_name("nhooo"))
new_name = comp_name #referencing a function with the object
print(new_name("nhooo"))
输出结果True
False
示例# Python program
# functions being passed as arguments to other functions
def new_inp(text):
return text.upper()
def old_inp(text):
return text.lower()
def display(func):
# storing the function in a normal variable
code = func("Love Coding, Learn everything on nhooo.com")
print (code)
display(new_inp) #directly referenced by passing functions as arguments.
display(old_inp)
输出结果LOVE CODING, LEARN EVERYTHING ON nhooo.com
love coding, learn everything on nhooo.com
在这里可以清楚地看到Python函数可以使用对象进行引用,也可以作为参数传递给另一个函数,这清楚地表明Python函数是First Class Citizens,可以使用对象实体进行引用和取消引用。
结论
在本文中,我们学习了标准Python库中包含的max和min函数的实现。