相关疑难解决方法(0)

python类定义

有一段时间我看到一些类被定义为对象类的子类,如

class my_class(object):
    pass
Run Code Online (Sandbox Code Playgroud)

如果与简单定义不同,如何呢?

class my_class():
    pass
Run Code Online (Sandbox Code Playgroud)

python

4
推荐指数
1
解决办法
1641
查看次数

在python3中使用没有继承类的super

有人知道下面的代码意味着什么super(xxx, self).__init__()吗?它有什么作用?我虽然它应该是 ABC 类继承自其他类,但我没有看到继承自哪个类。

class ABC:

    def __init__(self):
        super(ABC, self).__init__()
Run Code Online (Sandbox Code Playgroud)

python python-3.x

4
推荐指数
1
解决办法
381
查看次数

Python类声明的区别

在python类声明中,我可以通过几种方式声明一个类.以下样品有什么区别?

class MyClass:
 def __init__(self)
     pass


class MyClass(object):
   def __init__(self)
     pass
Run Code Online (Sandbox Code Playgroud)

python python-2.6

3
推荐指数
1
解决办法
809
查看次数

检查类是否可迭代

我有一个fib下面给出的课程.它实现__iter____next__.它是一个可迭代的,也是它自己的迭代器.

class fib(object):
    def __init__(self):
        self.prev = 0
        self.curr = 1

    def __iter__(self):
        return self

    def __next__(self):
        value = self.curr
        self.curr += self.prev
        self.prev = value
        return value


from collections import Iterable

print(isinstance(fib, Iterable))
Run Code Online (Sandbox Code Playgroud)

print语句返回False,我希望它返回True

python iterable class python-3.x

2
推荐指数
1
解决办法
3203
查看次数

Python中的类对象和类类型

我是 Python 新手,我有一个关于 OOP 的问题。因此,当我们创建一个类时,我们在括号中写入“对象”。例如,

\n
class My(object):\n
Run Code Online (Sandbox Code Playgroud)\n

我在某处读到这意味着该类继承自objectPython 中的类。但我也读到,我们创建的每个类都是该类的一个实例type。所以我的问题是object类与类相同type,如果是,为什么当要求Python打印类的类型时,它返回<class \'type\'>而不是返回<class \'object\'>。另外,为什么我们可以\xe2\x80\x99t写

\n
class My(type):\n
Run Code Online (Sandbox Code Playgroud)\n

如果object班级和type班级是同一个?

\n

python class

2
推荐指数
1
解决办法
108
查看次数

python3中定义的__new__在哪里?

在Python 2中:

>>> class A:
...  pass
... 
>>> A.__new__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: class A has no attribute '__new__'
>>> class A(object):
...  pass
... 
>>> A.__new__
<built-in method __new__ of type object at 0x1062fe2a0>
Run Code Online (Sandbox Code Playgroud)

结论:object包含__new__和子类继承该方法.

在Python 3中:

>>> class A:
...  pass
... 
>>> A.__new__
<built-in method __new__ of type object at 0x100229940>
Run Code Online (Sandbox Code Playgroud)

__new__是我们类中定义的方法,没有任何继承.这是如何运作的?__new__"来自哪里"?

python

1
推荐指数
1
解决办法
197
查看次数

AttributeError:类型对象“DirectView”没有属性“as_view”

这是我的views.py代码

class DirectView(mixins.CreateModelMixin):
    serializer_class=DirectSerializer
    def perform_create(self, serializer):
        serializer.save(user=self.request.user)
    def post(self,request,*args,**kwargs):
        return self.create(request,*args,**kwargs)
Run Code Online (Sandbox Code Playgroud)

和我的 urls.py

path('direct/',DirectView.as_view(),name='direct'),
Run Code Online (Sandbox Code Playgroud)

但每当我尝试运行服务器时,我都会收到错误

AttributeError: type object 'DirectView' has no attribute 'as_view'
Run Code Online (Sandbox Code Playgroud)

我不明白问题是什么?

python django django-rest-framework

1
推荐指数
1
解决办法
1792
查看次数

继承类中的python缺少属性

我得到一个奇怪的问题,其中一个类有一个score_card属性,另一个类使用继承它super().__init__,但对象实例说,它并没有有一个score_card属性.

以下示例有效:

 class ScoreCard:
    def report(self, severity, message):
        print('report.')

class Typeable:
    @property
    def type(self):
        return self._type
    @type.setter
    def type(self, new_type):
        self._type = new_type

class Node(Typeable):
    def __init__(self, dic):
        self.score_card = ScoreCard()

class Definition(Node):
    def __init__(self, dic):
        self.type = "definition"
        super().__init__(dic)
        self.score_card.report("critical", "NEEDS_NAME")

class Axiom(Definition):
    def __init__(self, dic):
        self.type = "axiom"
        super().__init__(dic)




#######################


a = Axiom({})
Run Code Online (Sandbox Code Playgroud)

但更大的程序中相同的代码并不能正常工作.必须有一些完全意想不到的东西发挥作用:

 ############################# IMPORTS ############################
import sys
from warnings import warn
import subprocess
import …
Run Code Online (Sandbox Code Playgroud)

python python-3.x

-2
推荐指数
1
解决办法
583
查看次数