magic-methods

    0熱度

    1回答

    我有一個任務,我應該創建一個類分數和所有的功能是「魔術方法」。問題是我的教授從來沒有教過我們神奇的方法。我已經使用 __init__ 但從未使用任何其他魔術方法。我有兩天的時間來編寫這個代碼,我需要一些關於構建代碼的指導。最近我所需要的編程,我發現在這裏:https://gist.github.com/mustaa/2350807 但我不明白什麼是代碼回事 的魔術方法是: class Frac

    0熱度

    1回答

    如果我在Python class Boo(object): def __init__(self, x): self._x = x self.__bingo = 3 self._info = {'x': x, 'y':42} def __dir__(self): return ['x'] @property de

    1熱度

    3回答

    我定義了一個我喜歡基本操作的類,如+,-,甚至更高級的數學運算,如sin來支持。也就是說,我需要爲這些基本的運算符和數學函數定義一些新的規則。 我可以處理使用Python的魔術方法如下基礎運營商, class NewObj(): def __init__(self, *args): pass def __add__(self, other): pas

    0熱度

    1回答

    我使用PHP 5.6以下的方法,它總是能正常工作 public function __get($name){ if(!empty($this->_dynamicFields[$name])){ if(!empty($this->_dynamicData[$name])){ return $this->_dynamicData[$name];

    14熱度

    3回答

    我有一個自定義類, class A: def __init__(self, a, b): self.a = a self.b = b 類是不迭代或可轉位或類似的東西。如果可能的話,我想保持這種狀態。是否有可能有以下工作? >>> x = A(1, 2) >>> min(x) 1 >>> max(x) 2 什麼讓我想起了這是min和max都在docs

    -1熱度

    2回答

    我正在尋找適當的解決方案,我已經做了這段代碼。我只想在打印XML時打印我的XML。 from xml.dom.minidom import Document, DOMImplementation class MyClass(object): init(): pass def create_xml(): doc = Document() # I know i

    2熱度

    1回答

    我已經定義了一個類,我試圖讓它可以被哈希化。此外,還有一個枚舉,它使用此類的對象作爲其枚舉成員的值。 from enum import Enum class Dummy(object): def __init__(self, name, property_): self.name = name # can only be a string self.p

    1熱度

    2回答

    代碼: class Pizza: def __init__(self, toppings): self.toppings = toppings self._pineapple_allowed = False @property def pineapple_allowed(self): return self._pineapple_all

    6熱度

    3回答

    說我已經建立了包含Foo類庫,支持一些神奇的方法,說__add__()和__radd__(): >>> class Foo(object): ... def __add__(self, rhs): ... print("Foo.__add__", rhs) ... def __radd__(self, lhs): ... print("Foo.__radd__", lhs)

    0熱度

    1回答

    這是什麼魔力呢? public function index() { $products = Product::all(); return view('products.index')->withProducts($products); } 還有就是View::make方式,這需要一個數組作爲第二個參數並且在with()方式,這需要串或陣列。 with('produc