Reputation: 3
I have an existing class which I am getting a dictionary as a result, if i will assign the method loop_vcenters to avariable name d
class vmware:
def loop_vcenters(self,vcenter_name):
si = SmartConnect(host = vcenter_name,user = 'username',pwd = 'password' ,sslContext=context)
atexit.register(Disconnect, si)
content = si.RetrieveContent()
cluster_host_dic_list=[]
cluster_name = ""
for cluster_obj in get_obj(content, vim.ComputeResource):
cluster=cluster_obj.name
hosts=[]
for host in cluster_obj.host:
hosts.append(host.name)
cluster_dic={cluster:hosts}
cluster_host_dic_list.append(cluster_dic)
return cluster_host_dic_list
x = vmware()
d = x.loop_vcenters('vcenter_name')
print(d) #will print the dictionary
I am trying to move si,atexit,content,cluster_host_dic_list,cluster_name
To be outside of the loop_vcenters function to act as global variables in the class as shown below:
class vmware:
vcenters = 'vcenter_name'
si = SmartConnect(host = vcenter_name,user = 'username',pwd = 'password' ,sslContext=context)
atexit.register(Disconnect, si)
content = si.RetrieveContent()
cluster_host_dic_list=[]
cluster_name = ""
def loop_vcenters(self):
for cluster_obj in get_obj(content, vim.ComputeResource):
cluster=cluster_obj.name
hosts=[]
for host in cluster_obj.host:
hosts.append(host.name)
cluster_dic={cluster:hosts}
cluster_host_dic_list.append(cluster_dic)
return cluster_host_dic_list
now when am assiging the loop_vcenters method i am getting:
without pertussis
x = vcenter_actions()
d = x.loop_vcenters
print(d)
<bound method vcenter_actions.loop_vcenters of <__main__.vcenter_actions instance at 0x7fd30ea95098>>
or with pertussis
d = x.loop_vcenters()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 9, in loop_vcenters
NameError: global name 'content' is not defined
What i am doing wrong ?
Upvotes: 0
Views: 66
Reputation: 1
Just to add more explanations and write few more options to the answer.. //module.py class A: x=1 This attribute is a "static attribute" in the class, you can reference to this out off the class just by call A.x without require to create an object. Like comment before you need to add self in the method before the variable use (self.x). There is another option, is to declare attribute in constructor,like: def init(self): self.x="1" In this option the attribute is per object and not per class. (So changes apply just in the current object) In case, you need global variable (in module file), you can just write the variable in the module like: //module.py x=1 class A: ... ... and you can use this global variable any place... pd: if you need to change the value you need to add global x, other is just read only.
Upvotes: 0
Reputation: 747
To expand on my comment: Mini Example
>>> class x:
... a = 3
... def print_a(self):
... print(a)
...
>>> x().print_a()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in print_a
NameError: global name 'a' is not defined
>>> class x:
... a = 3
... def print_a(self):
... print(self.a)
...
>>> x().print_a()
3
Though you might also just want to reference it by the class directly so print(x.a)
Upvotes: 1