面向对象的函数参数来改变变量 [英] Object orientated function parameter to alter variables

查看:107
本文介绍了面向对象的函数参数来改变变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将我的程序转换为面向对象的风格。目前,我有一个brandSelector()函数,它接受查询输入并查找查询和品牌数组之间的交集。但是,我试图将brandSelector()转换为通用Selector()或交集查找器。例如,我可以使用类型和查询(选择器(类型,查询))进行调用。在这种情况下,您可以键入Selector(品牌,查询)。额外的参数定义了哪个数组被检查以及设置了哪个变量 - 或者,if语句就足够了 - 但我不确定如何实现这样的系统。我将不胜感激任何解决方案或建议。谢谢,

 品牌= [apple,android,windows] 
品牌=无
$ b $ def Main():
query = input(输入您的查询:).lower()
brand = brandSelector(查询)
$ b $ def brandSelector (query):
try:
brand = set(brands).intersection(query.split())
brand =','.join(brand)
#Check Condition设置
后int_cond = confirmIntersection(品牌)
如果int_cond == False:
NameError(无交集)
返回品牌
除NameError:
print(\\\
no查询定义的品牌和品牌阵列\\\
之间找到的交集)
return brandDetectionFailure()

完整代码: https://github.com/ KentCoding / phoneTroubleshooting / blob / master / phoneTroubleshooting.py
Pyt hon版本:v3.5.2

我的尝试:

  def Main ():
init()
query = input(输入您的查询:).lower()
品牌=选择器(品牌,品牌,查询)
keywordSelection ,品牌)
def Selector(var,array,query):
试试:
#Format Brands查询
var = set(array).intersection(query.split())
var =','.join(var)
#设置
后的检查条件int_cond = confirmIntersection(var)
if int_cond == False:
raise NameError 无交集)
返回var
,除了NameError:
print(\\\
no在查询定义的品牌和品牌array \\\
之间找到交集)
return brandDetectionFailure()

但给出错误:
UnboundLocalError:local variable'brand '在分配前引用

解决方案 var 的参数传递给 Selector(),所以只需使用 def Selector(array,query):并用 brand = Selector(品牌,查询)调用它


I am trying convert my program to an object orientated style. Currently, I have a brandSelector() function which takes the query input and finds an intersection between the query and the brands array. However, I am trying to convert the brandSelector() to a generic Selector() or intersection finder. For example, I could call with type and the query (Selector(type, query)). In this instance you would type Selector(brand, query). That additional parameter defines which array is checked and which variable is set - alternatively, an if statement would suffice - but I am not sure how to implement such a system. I would appreciate any solutions or advice. Thanks,

brands = ["apple", "android", "windows"]
brand = None

def Main():
    query = input("Enter your query: ").lower()
    brand = brandSelector(query)

def brandSelector(query):
    try:
        brand = set(brands).intersection(query.split())
        brand = ', '.join(brand)
        #Check Condition After Setting
        int_cond = confirmIntersection(brand)
        if int_cond == False:
            raise NameError("No Intersection")
        return brand
    except NameError:
        print("\nNo Intersection found between query defined brand and brands array\n")
        return brandDetectionFailure()

Full Code: https://github.com/KentCoding/phoneTroubleshooting/blob/master/phoneTroubleshooting.py Python Version: v3.5.2

My Attempt:

def Main():
    init()
    query = input("Enter your query: ").lower()
    brand = Selector(brand, brands, query)
    keywordSelection(query, brand)
def Selector(var, array, query):
    try:
        #Format Brands Query
        var = set(array).intersection(query.split())
        var = ', '.join(var)
        #Check Condition After Setting
        int_cond = confirmIntersection(var)
        if int_cond == False:
            raise NameError("No Intersection")
        return var
    except NameError:
        print("\nNo Intersection found between query defined brand and brands array\n")
        return brandDetectionFailure()

But gives the error: UnboundLocalError: local variable 'brand' referenced before assignment

解决方案

From what I can see, there's no reason to have the argument named var being passed to Selector(), so just make it def Selector(array, query): and call it with brand = Selector(brands, query).

这篇关于面向对象的函数参数来改变变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆