本文整理匯總了Python中Spam.Eggs方法的典型用法代碼示例。如果您正苦於以下問題:Python Spam.Eggs方法的具體用法?Python Spam.Eggs怎麽用?Python Spam.Eggs使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類Spam
的用法示例。
在下文中一共展示了Spam.Eggs方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_spam
# 需要導入模塊: import Spam [as 別名]
# 或者: from Spam import Eggs [as 別名]
def test_spam(self):
class Eggs:
def get_yolks(self):
return self.yolks
m = new.module('Spam')
m.Eggs = Eggs
sys.modules['Spam'] = m
import Spam
def get_more_yolks(self):
return self.yolks + 3
# new.classobj()
C = new.classobj('Spam', (Spam.Eggs,), {'get_more_yolks': get_more_yolks})
# new.instance()
c = new.instance(C, {'yolks': 3})
o = new.instance(C)
self.assertEqual(o.__dict__, {}, "new __dict__ should be empty")
del o
o = new.instance(C, None)
self.assertEqual(o.__dict__, {}, "new __dict__ should be empty")
del o
def break_yolks(self):
self.yolks = self.yolks - 2
# new.instancemethod()
im = new.instancemethod(break_yolks, c, C)
self.assertEqual(c.get_yolks(), 3,
'Broken call of hand-crafted class instance')
self.assertEqual(c.get_more_yolks(), 6,
'Broken call of hand-crafted class instance')
im()
self.assertEqual(c.get_yolks(), 1,
'Broken call of hand-crafted instance method')
self.assertEqual(c.get_more_yolks(), 4,
'Broken call of hand-crafted instance method')
im = new.instancemethod(break_yolks, c)
im()
self.assertEqual(c.get_yolks(), -1)
# Verify that dangerous instance method creation is forbidden
self.assertRaises(TypeError, new.instancemethod, break_yolks, None)
# Verify that instancemethod() doesn't allow keyword args
self.assertRaises(TypeError, new.instancemethod, break_yolks, c, kw=1)