擅长:python、mysql、java
<p>我想你需要一个类变量</p>
<blockquote>
<p>that can be used globally later on in all the methods of my class</p>
</blockquote>
<p>当您使用<code>self.url</code>时,如果实例中没有<code>url</code>属性,它将引用类变量。它由所有实例共享。如果在某些实例上定义<code>self.url</code>,则它们的<code>self.url</code>将被隐藏</p>
<pre class="lang-py prettyprint-override"><code>class MyClass:
url = None
def my_fun(self):
print(self.url)
# code using the url
if __name__ == '__main__':
# list of integers
my_list = [1, 2, 3, 4, 5]
count = 0
my_object = MyClass()
for item in my_list:
if count % 2 == 0:
MyClass.url = "http://myurl.com" # I want to set/reset my url here
my_object.my_fun()
</code></pre>