仍在苦苦挣扎,如何使用list-element作为名称? [英] still struggling, howto use a list-element as a name ?

查看:59
本文介绍了仍在苦苦挣扎,如何使用list-element作为名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的示例中,pin表示pin。是一个具有许多属性的对象。

现在我想要

1-一种简单的方法来创建包含许多这些pin的对象

2-访问这些pin的多种方式,即

device.pin [some_index]

device.some_logical_name

ad 1:

a字典(在示例中为pinlist)似乎非常方便

方式(从设备创建者的角度来看)。

正如您在__ init __中看到的那样这段字典很容易被运送到pin-objects上。


ad 2:

这就是问题所在:如何自动化这些行self.GND = self.pin [0]


我也在寻找其他解决方案。


谢谢,

Stef

级Power_Supply(设备):

pinlist = {

0: (''GND'',_ DIG_OUT,_par2),

1:(''VCC'',_ DIG_OUT,_par33)

}


def __init __(自我):

#存储引脚中的引脚名称和引脚参数

for self in self.pinlist.keys():

self.pin [k] .Name = self.pinlist [k] [0]

self.pin [k] .Value = self.pinlist [k] [2]


#对于某些引脚,我们也希望能够使用逻辑名称

#如何使用像

# self.pinlist [0] = self.pin [0]"

#INSTEAD OF

self.GND = self.pin [0]

self.VCC = self.pin [1]


#创建一个Power_Supply实例和

#test如果引脚可以引用

Power = Power_Supply()

netlist1 =(Power.VCC,Power .pin [1],Power.GND,Power.pin [0])

解决方案

Stef Mientki< S. ****** ********@mailbox.kun.nlwrote在

新闻:b7 ************************ ***@news.speedlinq。 nl:


在下面的示例中,pin表示是一个具有多个

属性的对象。现在我想要

1-一种简单的方法来创建包含许多这些

" pin"的对象。 2-访问这些pin的多种方式,即

device.pin [some_index]

device.some_logical_name

ad 1:

a字典(在示例中为pinlist)似乎非常方便

方式(从设备创建者的角度来看)。

正如您在__ init __中所看到的那样这个字典可以很容易地将这个字典传送给pin-objects。


ad 2:

这就是问题所在:如何自动化这些行self.GND =

self.pin [0]"


我也在寻找其他解决方案。


谢谢,

Stef


类Power_Supply(设备):

pinlist = {

0:(''GND'',_ DIG_OUT,_par2),

1:(''VCC'',_ DIG_OUT,_par33)

}


def __init __(自我):

#存储引脚中的引脚名称和引脚参数

for k in self.pinlist.keys():

self.pin [k] .Name = self.pinlist [k] [0]

self.pin [k] .Value = self.pinlist [k] [2]


#对于某些引脚,我们也希望能够使用逻辑

名称#HOW TO使用类似于

#" self.pinlist [0] = self.pin [0]&q uot;

#INSTEAD OF

self.GND = self.pin [0]

self.VCC = self.pin [1]


#创建一个Power_Supply实例和

#test如果引脚可以引用

Power = Power_Supply()

netlist1 =(Power.VCC,Power.pin [1],Power.GND,Power.pin [0])



我可能会感到困惑你在追求什么,但不会像这项工作一样吗? (我不知道_par2对象是什么;我在这里命名了

。)


类Power_Supply(设备):

def __init __(自我):

self.pin = {

0:dict(名称=''GND'',值= _DIG_OUT, something = _par2),

1:dict(Name =''VCC'',Value = _DIG_OUT,something = _par33),

}

for self.pin.keys():

self .__ dict __ [self.pin [k] [''Name'']] = self.pin [k]


>类Power_Supply(设备):


> pinlist = {
0:(''GND'',_ DIG_OUT,_par2),
1:(''VCC'',_ DIG_OUT,_par33)
}


我可能会对你所追求的事情感到困惑,但不会像这项工作一样吗? (我不知道_par2对象是什么;我在这里命名为

。)



_par2,is只是(参考)一个常数


>

class Power_Supply(device):

def __init__ (个体经营):

self.pin = {

0:dict(Name =''GND'',Value = _DIG_OUT,something = _par2),

1:dict(Name =''VCC'',Value = _DIG_OUT,something = _par33),

}



为什么这么复杂,我需要10个或更多参数(或空),

然后这变得完全不可读。

因为这是用户界面的一部分,

(我希望Python人完全不知道,

写这些行),

我认为我的pinlist更容易。


for self in self.pin.keys():

self .__ dict __ [self.pin [k] [''姓名'']] = self.pin [k]



谢谢rzed ?,

正是我所寻找的:

self .__ dict __ [self.pinlist [k] [0]] = self.pin [k]

欢呼,

Stef


Stef Mientkiaécrit:


在下面的示例中,pin表示pin。是一个具有许多属性的对象。

现在我想要

1-一种简单的方法来创建包含许多这些pin的对象

2-访问这些pin的多种方式,即

device.pin [some_index]

device.some_logical_name

ad 1:

a字典(在示例中为pinlist)似乎非常方便

方式(从设备创建者的角度来看)。

正如您在__ init __中看到的那样这段字典很容易被运送到pin-objects上。


ad 2:

这就是问题所在:如何自动化这些行self.GND = self.pin [0]


我也在寻找其他解决方案。



我恐怕我不理解你的设计(也不是FWIW域名)。一些

评论无论如何:


>

class Power_Supply(device):



请重读我对前一个帖子中命名约定的评论...


pinlist = {



这不是*列表,因此命名为''pinlist''会产生误导。另外,为什么这里定义了



0 :(''GND'',_ DIG_OUT,_par2),

1:(''VCC'',_ DIG_OUT,_par33)

}


def __init __(个体经营):

#将引脚名称和引脚参数存储在引脚中

表示自己在.inlist.keys()中的k:

self.pin [k] .Name = self .pinlist [k] [0]



什么是''self.pin''?它在哪里定义? (注意:请尝试发布

* runnable *代码)。


和FWIW,如果它是一个容器,为什么它被命名为''pin '',而不是''引脚''?


self.pin [k] .Value = self.pinlist [k] [2]



这里使用for循环的合适方法是:

代表k,v代表self.pinlist.items():

self.pin [k] .name = v [0]

#etc


#对于某些引脚,我们也希望能够使用逻辑名称

#如何使用某些东西像

#" self.pinlist [0] = self.pin [0]"

#INSTEAD OF

self.GND = self.pin [0]

self.VCC = self.pin [1]



你可以构建一个''反向索引'':

#存储引脚中的引脚名称和引脚参数

for k,v in self.pinlist.items():

self.pin [k] .name = v [0]

self.pin [k] .value = v [2]

self.reversed_index [v [0]] = self.pin [k]


然后使用__getattr__钩子:


def __getattr __(自我,名字):

返回自我。 revers_index [name]


但是整个事情看起来仍然让我感到非常震惊和愚蠢,并且

我怀疑严重的设计缺陷...为什么不''你试着解释一下你的实际问题,而不是问如何实现你认为的*是
解决方案吗?


In the example below, "pin" is an object with a number of properties.
Now I want
1- an easy way to create objects that contains a number of these "pin"
2- an multiple way to access these "pin", i.e.
device.pin[some_index]
device.some_logical_name
ad 1:
a dictionary (as "pinlist" in the example) seems a very convenient
way (from a viewpoint of the device creator).
As you can see in the "__init__" section this dictionary can easily be
transported to the pin-objects.

ad 2:
THAT''s the problem: how do automate these lines "self.GND = self.pin[0]"

I''m also in for other solutions.

thanks,
Stef
class Power_Supply(device):
pinlist = {
0: (''GND'', _DIG_OUT, _par2),
1: (''VCC'', _DIG_OUT, _par33)
}

def __init__(self):
# store pin-names and pin-parameters in pins
for k in self.pinlist.keys():
self.pin[k].Name = self.pinlist[k][0]
self.pin[k].Value = self.pinlist[k][2]

# for some pins, we also want to be able to use logical names
# HOW TO USE SOMETHING like
# "self.pinlist[0] = self.pin[0]"
# INSTEAD OF
self.GND = self.pin[0]
self.VCC = self.pin[1]

# create a Power_Supply instance and
# test if pins can be referenced in
Power = Power_Supply()
netlist1 = ( Power.VCC, Power.pin[1], Power.GND, Power.pin[0] )

解决方案

Stef Mientki <S.**************@mailbox.kun.nlwrote in
news:b7***************************@news.speedlinq. nl:

In the example below, "pin" is an object with a number of
properties. Now I want
1- an easy way to create objects that contains a number of these
"pin" 2- an multiple way to access these "pin", i.e.
device.pin[some_index]
device.some_logical_name
ad 1:
a dictionary (as "pinlist" in the example) seems a very
convenient
way (from a viewpoint of the device creator).
As you can see in the "__init__" section this dictionary can
easily be transported to the pin-objects.

ad 2:
THAT''s the problem: how do automate these lines "self.GND =
self.pin[0]"

I''m also in for other solutions.

thanks,
Stef
class Power_Supply(device):
pinlist = {
0: (''GND'', _DIG_OUT, _par2),
1: (''VCC'', _DIG_OUT, _par33)
}

def __init__(self):
# store pin-names and pin-parameters in pins
for k in self.pinlist.keys():
self.pin[k].Name = self.pinlist[k][0]
self.pin[k].Value = self.pinlist[k][2]

# for some pins, we also want to be able to use logical
names # HOW TO USE SOMETHING like
# "self.pinlist[0] = self.pin[0]"
# INSTEAD OF
self.GND = self.pin[0]
self.VCC = self.pin[1]

# create a Power_Supply instance and
# test if pins can be referenced in
Power = Power_Supply()
netlist1 = ( Power.VCC, Power.pin[1], Power.GND, Power.pin[0] )

I may be confused about what you''re after, but wouldn''t something
like this work? (I don''t know what a _par2 object is; I''ve named
it something here.)

class Power_Supply(device):
def __init__(self):
self.pin = {
0:dict(Name=''GND'',Value=_DIG_OUT,something=_par2),
1:dict(Name=''VCC'',Value=_DIG_OUT,something=_par33) ,
}
for k in self.pin.keys():
self.__dict__[self.pin[k][''Name'']] = self.pin[k]


>class Power_Supply(device):

> pinlist = {
0: (''GND'', _DIG_OUT, _par2),
1: (''VCC'', _DIG_OUT, _par33)
}


I may be confused about what you''re after, but wouldn''t something
like this work? (I don''t know what a _par2 object is; I''ve named
it something here.)

_par2, is just (a reference to) a constant

>
class Power_Supply(device):
def __init__(self):
self.pin = {
0:dict(Name=''GND'',Value=_DIG_OUT,something=_par2),
1:dict(Name=''VCC'',Value=_DIG_OUT,something=_par33) ,
}

Why so complex, I need 10 or more parameters (or empty),
and then this becomes completely unreadable.
As this is part of the "user interface",
(I want that completely unknown with Python people,
write these lines),
I think my "pinlist" is much easier.

for k in self.pin.keys():
self.__dict__[self.pin[k][''Name'']] = self.pin[k]

thanks "rzed" ?,
that is exactly what I was looking for:
self.__dict__[self.pinlist[k][0]] = self.pin[k]
cheers,
Stef


Stef Mientki a écrit :

In the example below, "pin" is an object with a number of properties.
Now I want
1- an easy way to create objects that contains a number of these "pin"
2- an multiple way to access these "pin", i.e.
device.pin[some_index]
device.some_logical_name
ad 1:
a dictionary (as "pinlist" in the example) seems a very convenient
way (from a viewpoint of the device creator).
As you can see in the "__init__" section this dictionary can easily be
transported to the pin-objects.

ad 2:
THAT''s the problem: how do automate these lines "self.GND = self.pin[0]"

I''m also in for other solutions.

I''m afraid I don''t understand your design (nor the domain FWIW). A few
comments anyway:

>
class Power_Supply(device):

Please reread my comments about naming convention in a previous thread...

pinlist = {

This is *not* a list, so naming it ''pinlist'' is misleading. Also, why is
this defined here ?

0: (''GND'', _DIG_OUT, _par2),
1: (''VCC'', _DIG_OUT, _par33)
}

def __init__(self):
# store pin-names and pin-parameters in pins
for k in self.pinlist.keys():
self.pin[k].Name = self.pinlist[k][0]

What is ''self.pin'' ? Where is it defined ? (NB : please try to post
*runnable* code).

And FWIW, if it''s a container, why is it named ''pin'', and not ''pins'' ?

self.pin[k].Value = self.pinlist[k][2]

The appropriate way to use the for loop here is:
for k, v in self.pinlist.items():
self.pin[k].name = v[0]
# etc

# for some pins, we also want to be able to use logical names
# HOW TO USE SOMETHING like
# "self.pinlist[0] = self.pin[0]"
# INSTEAD OF
self.GND = self.pin[0]
self.VCC = self.pin[1]

you can build a ''reversed index'':
# store pin-names and pin-parameters in pins
for k, v in self.pinlist.items():
self.pin[k].name = v[0]
self.pin[k].value = v[2]
self.reversed_index[v[0]] = self.pin[k]

and then use the __getattr__ hook:

def __getattr__(self, name):
return self.reversed_index[name]

But the whole thing still looks awfully convulted and kludgy to me, and
I suspect serious design flaws... Why don''t you try and explain your
real problem, instead of asking how to implement what you *think* is the
solution ?


这篇关于仍在苦苦挣扎,如何使用list-element作为名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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