本文整理汇总了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)