猿问

Python:向实例添加属性,让它出现在类中

根据您的代码,您告诉for循环迭代 a String,因为您将列表转换为带有User_string = str(Users);的字符串。所以循环遍历字符串上的每个字符,现在是User01User02User03


您需要做的是迭代 list Users,例如:


from arcgis.gis import GIS


Users = ['User01','User02','User03']

gis = GIS("https://www.arcgis.com","USERNAME","PASSWORD") # This logs you into ArcGIS Online

User_role = 'org_user'


for x in Users:

    test = gis.users.get(username=x)

    test.update_role(role=User_role)

  print("Done! Check Web")


红颜莎娜
浏览 240回答 2
2回答

HUH函数

以 Octave https://octave.org/doc/v4.4.1/Structure-Arrays.html为例制作一个结构数组:>> x(1).a = "string1";>> x(2).a = "string2";>> x(1).b = 1;>> x(2).b = 2;>>>> xx =&nbsp; 1x2 struct array containing the fields:&nbsp; &nbsp; a&nbsp; &nbsp; b如果我向一个条目添加一个字段,则会为另一个条目添加或定义一个默认值:>> x(1).c = 'red'x =&nbsp; 1x2 struct array containing the fields:&nbsp; &nbsp; a&nbsp; &nbsp; b&nbsp; &nbsp; c>> x(2)ans =&nbsp; scalar structure containing the fields:&nbsp; &nbsp; a = string2&nbsp; &nbsp; b =&nbsp; 2&nbsp; &nbsp; c = [](0x0)>> save -7 struct1.mat x在 numpyIn [549]: dat = io.loadmat('struct1.mat')In [550]: datOut[550]:&nbsp;{'__header__': b'MATLAB 5.0 MAT-file, written by Octave 4.2.2, 2019-02-09 18:42:35 UTC',&nbsp;'__version__': '1.0',&nbsp;'__globals__': [],&nbsp;'x': ...In [551]: dat['x']Out[551]:&nbsp;array([[(array(['string1'], dtype='<U7'), array([[1.]]), array(['red'], dtype='<U3')),&nbsp; &nbsp; &nbsp; &nbsp; (array(['string2'], dtype='<U7'), array([[2.]]), array([], shape=(0, 0), dtype=float64))]],&nbsp; &nbsp; &nbsp; dtype=[('a', 'O'), ('b', 'O'), ('c', 'O')])In [552]: _.shapeOut[552]: (1, 2)该 struct 已转换为结构化的 numpy 数组,与shapeOctave相同size(x)。每个 struct 字段都是dat.与 Octave/MATLAB 相比,我们不能dat['x']就地添加字段。我认为有一个函数import numpy.lib.recfunctions as rf可以添加一个字段,具有各种形式的掩码或未定义值的默认值,但这将创建一个新数组。通过一些工作,我可以从头开始。In [560]: x1 = rf.append_fields(x, 'd', [10.0])In [561]: x1Out[561]:&nbsp;masked_array(data=[(array(['string1'], dtype='<U7'), array([[1.]]), array(['red'], dtype='<U3'), 10.0),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(array(['string2'], dtype='<U7'), array([[2.]]), array([], shape=(0, 0), dtype=float64), --)],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;mask=[(False, False, False, False),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(False, False, False,&nbsp; True)],&nbsp; &nbsp; &nbsp; &nbsp;fill_value=('?', '?', '?', 1.e+20),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dtype=[('a', 'O'), ('b', 'O'), ('c', 'O'), ('d', '<f8')])In [562]: x1['d']Out[562]:&nbsp;masked_array(data=[10.0, --],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;mask=[False,&nbsp; True],&nbsp; &nbsp; &nbsp; &nbsp;fill_value=1e+20)这种动作不太适合 Python 的类系统。一个类通常不会跟踪它的实例。并且一旦定义了一个类通常不会被修改。可以维护一个实例列表,也可以向现有类添加方法,但这不是常见的做法。

牧羊人nacy

该setattr方法x = "temperature"setattr(red,x,"HOT")我认为这就是你所要求的但也许您想要的是重载颜色类的__setattr__和__getattr__方法class color:&nbsp; &nbsp; &nbsp;attrs = {}&nbsp; &nbsp; &nbsp;def __getattr__(self,item):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if item in self.attrs:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return self.attrs[item]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return ""&nbsp; &nbsp; &nbsp;def __setattr__(self,attr,value):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;self.attrs[attr] = valuec = color()print(repr(c.hello))c.hello = 5print(repr(c.hello))print(repr(c.temperature))x = 'temperature'setattr(c,x,"HOT")print(repr(c.temperature))
随时随地看视频慕课网APP

相关分类

Python
我要回答