相关疑难解决方法(0)

在Python中覆盖__new__和__init__

可能重复:
Python使用__new__和__init__?

我理解它的方式__init__与Java中的构造函数不同,因为__init__只初始化已经隐式构造的对象(因为__init__之后调用__new__).但是,我曾经需要定义的所有东西都使用了Java中"构造函数"的后一个属性.程序员想要覆盖的情况是__new__什么?

编辑:为了记录,我问的部分是因为我想知道在这个问题的接受答案中,覆盖的优势/ 使用单独的类方法会有什么优势/劣势:

在Python中超越工厂

python

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

需要了解__init __,__ new__和__call__的流程

class Singleton(type):
    def __init__(self, *args, **kwargs):
        print 'calling __init__ of Singleton class', self
        print 'args: ', args
        print 'kwargs: ', kwargs
        super(Singleton, self).__init__(*args, **kwargs)
        self.__instance = None
    def __call__(self, *args, **kwargs):
        print 'running __call__ of Singleton', self
        print 'args: ', args
        print 'kwargs: ', kwargs, '\n\n'
        if self.__instance is None:
            self.__instance = super(Singleton, self).__call__(*args, **kwargs)
        return self.__instance

class A(object):
    __metaclass__ = Singleton
    def __init__(self,a):
        print 'in __init__ of A:  ', self
        self.a = a
        print 'self.a: ', self.a

a=A(10)
b=A(20) …
Run Code Online (Sandbox Code Playgroud)

python singleton design-patterns metaclass

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

PyCharm 中的 PyTorch 特定检查问题

有没有人能够解决 PyCharm 中 PyTorch 特定的检查问题?之前针对非 PyTorch 相关问题的帖子建议升级 PyCharm,但我目前使用的是最新版本。一种选择当然是完全禁用某些检查,但我宁愿避免这种情况。

示例:torch.LongTensor(x)给我“意外的参数...”,而两种调用签名(带和不带x)都受支持。

pycharm pytorch

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

Why should I use __new__ instead of __init__ in this example?

I have been studying __new__ recently, and I've read lots of code where __new__ is used instead of __init__. Sometimes I think both work, but I don't know why the original author uses __new__.

Can someone explain why the below code uses __new__ instead of __init__? I want a reason for this example. I know the difference between __new__ and __init__, but I don't know why __new__ is used here.

Example:

    class MiniSubtest(object):
        def __new__(cls, …
Run Code Online (Sandbox Code Playgroud)

python design-patterns

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

Python的__new__有哪些用例?

我理解是什么__new__(以及它与之有什么不同__init__)所以我对定义不感兴趣,我对何时以及如何使用感兴趣__new__.

文件说:

在一般情况下,你不应该需要重写__new__,除非你继承一个不变型像str,int,unicodetuple

但我无法想到使用其他情况__new__或如何正确使用它(例如,当子类化不可变类型或为什么在这种情况下需要它时).

那么,何时,为什么以及如何使用__new__

我对用例很感兴趣,而不是它的用途(我知道它的作用).

python

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

Python - 初始化程序与构造函数

我听说__init__python 中的函数不是构造函数,它是一个初始化器,实际上__new__函数是构造函数,不同之处在于__init__函数是在创建对象和__new__之前调用之后调用的.我对吗?你能解释的区别更好,我们为什么需要两个__new____init__

python constructor initializer

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

从构造函数返回类的实例

我需要在构造函数中返回 Bike 的实例。例如:

class Bike(object):
    def __init__(self,color):
        self.bikeColor = color
        return self #It should return an instance of the class; is this not right?

myBike = Bike("blue")
Run Code Online (Sandbox Code Playgroud)

当我执行上述操作时,出现以下错误:

TypeError: __init__() should return None, not 'Bike'
Run Code Online (Sandbox Code Playgroud)

如果是这种情况,如果只是假设的话,我该如何返回一个实例return None

python instance

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

__new__和__init__

可能重复:
Python使用__new__和__init__?

我试图了解如何使用这两种方法.我知道这__new__ 用于创建一个对象,同时__init__进行初始化.但是我不确定在创建对象时会发生什么.

  1. 这是否意味着__new____init__必须具有相同的参数?

  2. 如果我不使用相同的参数会发生什么?

python

5
推荐指数
2
解决办法
4418
查看次数

TypeError:__ init __()采用0位置参数,但给出了1

我刚刚开始在我的学校编写代码,我正在学习如何使用Python.我们老师给了我们这个任务:

  1. 创建一个类,并将其命名为"CustomerInfo".
  2. 创建一个没有参数的构造函数(仅限self).
  3. 为名称,订单,数量和地址创建用户输入.
  4. 为名称,顺序,数量和地址创建mutator方法.
  5. 为名称,订单,数量和地址创建存取方法.
  6. 创建一个新的CustomerInfo()对象并将其命名为"customer1".
  7. 打印出客户信息.

这是我的代码:

class CustomerInfo:

    def __init__ ():
        self.name = theName
        self.order = theOrder
        self.quantity = theQuantity
        self.address = theAddress

    def setName( self, newName ):
        self.Name = newName
    def setOrder ( self, newModel ):
        self.model = newModel
    def setQuantity ( self, newQuantity ):
        self.quantity = newQuantity
    def setAddress (self, newAddress ):
        self.address = newAddress

    def getName ( self ):
        return self.name
    def getOrder ( self ):
        return self.order
    def getQuantity ( self ):
        return …
Run Code Online (Sandbox Code Playgroud)

python methods class init typeerror

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

Python条件对象实例化

我正在参加在线 MOOC 课程,但我很难弄清楚这一点,甚至无法准确地表达我正在试图弄清楚的内容。问题是要求仅当某个字符串作为参数传入时才创建对象。您可以在这里看到问题的描述:https://docs.google.com/forms/d/1gt4McfP2ZZkI99JFaHIFcP26lddyTREq4pvDnl4tl0w/viewform ?c=0&w=1 具体部分在第三段中。使用“if”作为init的条件是否合法?谢谢。

python

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