Python class

Attribute
modname.the_ans 代表 the_ans 是 Object modname的一個attribute

modname.the_ans = 42
若 modname可讀寫的話,這樣的寫法是被允許的
當然也可以寫 del modname.the_ans 來移除 the_ans 這個 attribute


namespace
根據Python上定義:Namespaces are created at different moments and have different lifetimes. The namespace containing the built-in names is created when the Python interpreter starts up, and is never deleted.

Class objects support two kinds of operations: attribute references and instantiation.

class ClassName:
    <statement-1>
    .
    .
    .
    <statement-N>

class MyClass:
    i = 12345
    def f(self):
        return 'hello world'

class Complex:
 def __init__(self, realpart, imagpart):
  self.r = realpart
  self.i = imagpart

x = Complex(3.0, -4.5)
""" x.r, x.i ==> (3.0, -4.5) """

0 意見: