我是靠谱客的博主 鲜艳白昼,最近开发中收集的这篇文章主要介绍python访问网页变量,在Python中访问类的变量,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

class Example(object):

def the_example(self):

itsProblem = "problem"

theExample = Example()

print(theExample.itsProblem)

How do I access a class's variable? I've tried adding this definition:

def return_itsProblem(self):

return itsProblem

Yet, that fails also.

解决方案

The anwser, in a few words

In your example, itsProblem is a local variable.

Your must use self to set and get instance variables. You can set it in the __init__ method. Then your code would be:

class Example(object):

def __init__(self):

self.itsProblem = "problem"

theExample = Example()

print(theExample.itsProblem)

But if you want a true class variable, then use the class name directly:

class Example(object):

itsProblem = "problem"

theExample = Example()

print(theExample.itsProblem)

print (Example.itsProblem)

But be careful with this one, as theExample.itsProblem is automatically set to be equal to Example.itsProblem, but is not the same variable at all and can be changed independently.

Some explanations

In Python, variables car be created dynamically. Therefor, you can do the following:

class Example(object):

pass

Example.itsProblem = "problem"

e = Example()

e.itsSecondProblem = "problem"

print Example.itsProblem == e.itsSecondProblem

Prints

True

Therefore, that's exactly what you do with the previous examples.

Indeed, in Python we use self as this, but it's a bit more than that. Self is the the first argument to any object method because the first argument is always the object reference. This is automatic, whether you call it self or not.

Which means you can do:

class Example(object):

def __init__(self):

self.itsProblem = "problem"

theExample = Example()

print(theExample.itsProblem)

Or:

class Example(object):

def __init__(my_super_self):

my_super_self.itsProblem = "problem"

theExample = Example()

print(theExample.itsProblem)

It's exactly the same. The first argument of ANY object method is the current object, we only call it self as a convention. And you add just a variable to this object, the same way you would do it from outside.

Now, about the class variables.

When you do:

class Example(object):

itsProblem = "problem"

theExample = Example()

print(theExample.itsProblem)

You'll notice we first set a class variable, then we access an object (instance) variable. We never set this object variable but it works, how is that possible?

Well, Python tries to get first the object variable, but if it can't find it, will give you the class variable. Warning: the class variable is shared among instances, and the object variable is not.

As a conclusion, never use class variables to set default values to object variables. Use __init__ for that.

Eventually, you will learn that Python classes are instances and therefore objects themselves, which gives new insight to understanding the above. Come back and read this again later, once you realize that.

最后

以上就是鲜艳白昼为你收集整理的python访问网页变量,在Python中访问类的变量的全部内容,希望文章能够帮你解决python访问网页变量,在Python中访问类的变量所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(79)

评论列表共有 0 条评论

立即
投稿
返回
顶部