English 中文(简体)
解释甲型可变范围
原标题:Explaining Python variable scope
  • 时间:2010-04-20 18:11:58
  •  标签:
  • python
  • scope

我叫 myself自己是Zar,我正在把一些样本法翻译成这一文字。

class Student(object):
    def __init__( self, name, a,b,c ):
        self.name = name
        self.a = a
        self.b = b
        self.c = c

    def average(self):
        return ( a+b+c ) / 3.0 

这正是我的本意类别定义。

在主要方法中,我创立了一个例,称为<代码>a:

if __name__ == "__main__" :
    a = Student( "Oscar", 10, 10, 10 )

我如何发现,<代码>>所申报的可变代码<>a可在<代码>average上查到,并且为了使这一方法发挥作用,我必须打上<代码>本身.a + Self.c。

这样做的理由是什么?

最佳回答

Barenames(如a,b,c)一直被作为地方或全球范围(放弃职务,在您的法典中不存在任何地方)。 理由是,进一步扩大范围将毫无必要地使事情更加复杂——例如,如果在您的<代码>上,a = 一种 光名称<代码>a。 范围可以指你似乎想要的东西(相当于<代码>本身.a),因此,转让本身毫无意义(给自己指定名称),因此,你需要进一步复杂的规则。

如果你想要某种不同于简单、简单、简单和优化的行为,那么仅仅使用合格名称(如<代码>本身.a),这远是最简单的办法——完全可行、没有任何复杂规则,并且使汇编者能够有效地优化事情(例如,光名的范围总是有条理地确定,>>>/em>取决于环境的动态不同特性。 因此,除了对其他语文适用更为复杂的复印规则,实际上没有任何理由使光名的语种复杂化。

问题回答

有几个原因,尽管主要原因来自Zentar :“明示优于默示” 用C++这样的语言,这一类方法总是有一个隐含的论点:, 即, 每当使用这种方法时,该方法就被推向 st。 在这种情况下,如果存在一个实例变量<代码>b以及一个全球变量<代码>b,则用户仅可参阅b<>>>>>>> 系指一种,而不必认识到另一种应用。 因此,沙尔迫使你明确了解你的范围,以避免混乱。

尽管如此,也有其他原因。 例如,我可以确定一个班级以外的职能,然后在操作时间将职能归入一个班级。 例如:

def log(self):
    print "some library function requires all objects to have a log method"
    print "unfortunately we re using the Student class, which doesn t have one"
    print "this class is defined in a separate library, so we can t add the method"
    print "fortunately, we can just add the method dynamically at runtime"

Student.log = log

在这里,<代码>自封/代码”明确表明,我们界定某一类别以外的职能,然后将其归入这一类别,是微不足道的。 我常常不做这样的事,但是,我这样做时,它非常有用。

这里是一个更加复杂的例子;假设我们想在另一类内确定一个类别,例如为了单位测试的目的:

class SomeUnitTests(TestCase):
    def test_something(self):
        class SomeMockObject(SomeActualObject):
            def foo(self2):
                self.assertEqual(self2.x, SOME_CONSTANT)

        some_lib.do_something_with(SomeMockObject)

在这里,有明确的自私(我们可以称之为我们想要的东西,它不一定是自我的)允许区分内部和外部班子的<代码>。 同样,我经常这样做,但当我这样做时,这无疑是有用的。

应当使用自发的变量来指所有实例变量。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...