我正在使用python 2.7.9在Mac OS X Yosemite上工作.
这是我尝试过的:
定义一个类
class A:
def test(self):
print "test"
Run Code Online (Sandbox Code Playgroud)
然后运行
A.__mro__
Run Code Online (Sandbox Code Playgroud)
然后我得到了
>>> A.__mro__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: class A has no attribute '__mro__'
Run Code Online (Sandbox Code Playgroud)然后我定义
class B(object):
def test(self):
print "test"
Run Code Online (Sandbox Code Playgroud)
然后运行
B.__mro__
Run Code Online (Sandbox Code Playgroud)
然后我得到了
>>> B.__mro__
(<class '__main__.B'>, <type 'object'>)
Run Code Online (Sandbox Code Playgroud)这两个定义有什么不同?我发现在python 3中,没有"对象"的版本仍然有__mro__方法.
码:
import types
class C(object):
pass
c = C()
print(isinstance(c, types.InstanceType))
Run Code Online (Sandbox Code Playgroud)
输出:
False
Run Code Online (Sandbox Code Playgroud)
检查对象是否为新式类的用户定义类的实例的正确方法是什么?
如果检查对象的类型是否是用户定义的,我想要额外强调.根据文件:
types.InstanceType 用户定义的类
的实例类型.
好吧 - 不是"正确"的方式也可以.
还注意到set模块中没有类型types
我试图在类构造函数(init)中返回一个值:
class A:
def __init__(self):
return 1
Run Code Online (Sandbox Code Playgroud)
但是有一个运行时错误,说init应该返回None.如果是这种情况,如何理解:
a=A()
Run Code Online (Sandbox Code Playgroud)
其中"a"被指定为类实例?
我正在创建一个游戏,其中我有一个复杂的方法来创建实体.
加载级别时,加载代码会读取一堆YAML文件,这些文件包含所有不同可能单元的属性.使用YAML文件,它创建一个所谓的EntityResource对象.此EntityResource对象在生成新单元时充当信息的权威来源.目标是双重的:
EntityResource然后将这些对象馈送到EntityFactory对象中以生成特定类型的单元.
我的问题如下. 有没有办法EntityResource根据正在读入的YAML文件的内容动态创建子图?
另外,我希望为这些YAML文件派生的每个子类分配一个单独的元类.有什么警告吗?
下面两个类有什么区别?你有关于这个案子的一些相关信息吗?非常感谢你.
class test(object):
def __init__(self, name):
print name
class test():
def __init__(self, name):
print name
Run Code Online (Sandbox Code Playgroud) 当类从没有继承时,我有一个实例类型的对象.
>>> class A(): pass;
>>> a = A()
>>> type(a)
<type 'instance'>
>>> type(a) is A
False
>>> type(A)
<type 'classobj'>
Run Code Online (Sandbox Code Playgroud)
但是,当我从对象继承相同的类时,创建的对象是A的类型.
>>> class A(object): pass;
>>> a = A()
>>> type(a)
<class '__main__.A'>
>>> type(a) is A
True
>>> type(A)
<type 'type'>
Run Code Online (Sandbox Code Playgroud)
这背后的逻辑是什么?这是否意味着每个类都应该从对象继承?
我有以下代码:
>>> class MyClass:
pass
>>> myObj=MyClass()
>>> type(myObj)
<type 'instance'> <==== Why it is not type MyClass ?
>>> type(MyClass)
<type 'classobj'> <=== Why it is not just 'MyClass'?
>>> isinstance(myObj, instance) <==== Why the 'instance' is not defined?
Traceback (most recent call last):
File "<pyshell#91>", line 1, in <module>
isinstance(myObj, instance)
NameError: name 'instance' is not defined
>>> isinstance(myObj, MyClass)
True
>>> myObj.__class__
<class __main__.MyClass at 0x0000000002A44D68> <=== Why different from type(myObj) ?
Run Code Online (Sandbox Code Playgroud)
似乎Python 在类和它的实例类型之间有一些额外的间接.
我习惯了C#.在C#中, …
我读到构造函数就像传递给类的第一个参数一样,这对我来说很有意义,因为参数似乎是通过__init__方法传递给类的。例如,
class NewsStory(object):
def __init__(self, guid, title, subject, summary, link):
self.guid = guid
self.title = title
self.subject = subject
self.summary = summary
self.link = link
def get_guid(self):
return self.guid
def get_title(self):
return self.title
def get_subject(self):
return self.subject
def get_summary(self):
return self.summary
def get_link(self):
return self.link
firstStory = NewsStory('Globally Unique Identifier', \
'first_title','first_subject','This is an example \
sumary','example_link@something.com')
print firstStory.get_guid() # prints Globally Unique Identifier
Run Code Online (Sandbox Code Playgroud)
因此,当我“调用”该类时,是否将__init__方法中的参数传递给它?我是班上的新手,我读到的所有内容我都很难理解和混淆。谢谢!
编辑1
我发现这个问题有助于解释某些事情,例如new和init之间的区别,抱歉,我不知道如何添加链接,必须剪切和粘贴:__init__可以做什么,__new__不能呢?
在Python中创建一个类时,它应该继承自object还是Object,还是两者都不继承?是否有必要从 object 继承?
class NewClass(object)
Run Code Online (Sandbox Code Playgroud)
或者
class NewClass(Object)
Run Code Online (Sandbox Code Playgroud)
或者
class NewClass()
Run Code Online (Sandbox Code Playgroud) 我正在学习python并向OOP介绍自己.但是,我正在努力理解如何最好地构建类,特别是,以下类定义之间的差异以及何时应该使用每个类:
class my_class:
content...
class my_class():
content...
class my_class(object):
content...
Run Code Online (Sandbox Code Playgroud)
我一直在阅读非常有用的python在线帮助,虽然没有找到这个问题的具体答案.所以任何想法或推荐的参考将不胜感激,谢谢.
python ×10
class ×3
oop ×3
object ×2
constructor ×1
inheritance ×1
python-2.7 ×1
python-2.x ×1
singleton ×1
subclass ×1
subclassing ×1