Python def init return

Explicit return in __init__¶

__init__ is a special Python method that is automatically called when memory is allocated for a new object. The sole purpose of __init__ is to initialize the values of instance members for the new object. Using __init__ to return a value implies that a program is using __init__ to do something other than initialize the object. This logic should be moved to another instance method and called by the program later, after initialization.

Anti-pattern¶

The __init__ method of the Rectangle class below attempts to return the area of the rectangle within the __init__ method. This violates the rule of only using __init__ to initialize instance members.

class Rectangle: def __init__(self, width, height): self.width = width self.height = height self.area = width * height # causes "Explicit return in __init__" error return self.area 

Best practices¶

Remove the return statement from the __init__ method¶

Remove the return statement in the __init__ method that is returning a value.

class Rectangle: def __init__(self, width, height): self.width = width self.height = height self.area = width * height # return statement removed from here 

Move the program logic to another instance method¶

There is no reason why the Rectangle class MUST return the area immediately upon initialization. This program logic should be moved to a separate method of the Rectangle class. The program can call the method later, after the object has successfully initialized.

class Rectangle(object): def __init__(self, width, height): self.width = width self.height = height self._area = width * height @property # moved the logic for returning area to a separate method def area(self): return self._area 

Note that the class must inherit from object now, since the property decorator only works for new style classes.

Читайте также:  Абсолютное позиционирование

References¶

Источник

Как вернуть значение из __init__ в Python?

У меня есть класс с функцией __init__ . Как вернуть целочисленное значение из этой функции при создании объекта? Я написал программу, где __init__ выполняет синтаксический анализ командной строки, и мне нужно установить определенное значение. Можно ли установить его в глобальной переменной и использовать его в других функциях-членах? Если да, то как это сделать? До сих пор я объявлял переменную вне класса. и установка его одной функции не отражается в другой функции?

Пожалуйста, удалите свой комментарий и обновите свой вопрос. У вас есть вопрос. Это твой вопрос. Пожалуйста, исправьте вопрос, чтобы правильно показать, в чем заключается ваша настоящая проблема. Вы неправильно используете __init__ ; мы можем помочь вам, если вы опишите, чего вы на самом деле пытаетесь достичь.

9 ответов

__init__ требуется вернуть None. Вы не можете (или, по крайней мере, не должны) возвращать что-то еще.

Попробуйте сделать все, что хотите, чтобы вернуть переменную экземпляра (или функцию).

>>> class Foo: . def __init__(self): . return 42 . >>> foo = Foo() Traceback (most recent call last): File "", line 1, in TypeError: __init__() should return None 

init не возвращает вновь созданный объект — как видно из TypeError, требуется вернуть None, верно? Вновь созданный объект возвращается new , init просто устанавливает некоторые его атрибуты. Но да, как вы сказали, изменение init или new для возврата чего-то другого действительно не имеет смысла.

Где здесь new ? new неявное в Python? Я предположил, что семантика Python отличается от Java и других языков, которые используют это слово.

То, что это невозможно, не означает, что это не имеет смысла. Например, было бы неплохо передать данные из super().__init__ производному классу без необходимости передавать их через переменную экземпляра.

Если вы хотите вернуть какой-либо другой объект при вызове класса, используйте метод __new__() :

class MyClass(object): def __init__(self): print "never called in this case" def __new__(cls): return 42 obj = MyClass() print obj 

Да, new — это верный способ вернуть что-то отличное от экземпляра класса при использовании класса . Мне просто интересно — есть ли причина, по которой вы действительно хотите это сделать?

@weronika Одна идея: в любой ситуации, когда вы обычно используете фабрику, но у вас есть причина хотеть представить интерфейс, который выглядит как обычная реализация класса. Пример: добавляя некоторые новые необязательные параметры в __init__ вашего класса, вы понимаете, что для обеспечения необходимой гибкости вам нужна фабрика классов, которая возвращает экземпляры специализированных подклассов. Но пользователи вашей библиотеки уже используют ваш существующий API. Чтобы сохранить его, вы переопределяете __new__ для возврата экземпляров ваших специализированных подклассов.

Если вы хотите увидеть пример того, что сказал Марк Амери, посмотрите исходный код модуля datetime из стандартной библиотеки. Он использует __new__ именно так.

Также, если кто-то хочет сделать это, убедитесь, что он наследуется от объекта, иначе он не будет работать.

Источник

Explicit return in __init__¶

__init__ is a special Python method that is automatically called when memory is allocated for a new object. The sole purpose of __init__ is to initialize the values of instance members for the new object. Using __init__ to return a value implies that a program is using __init__ to do something other than initialize the object. This logic should be moved to another instance method and called by the program later, after initialization.

Anti-pattern¶

The __init__ method of the Rectangle class below attempts to return the area of the rectangle within the __init__ method. This violates the rule of only using __init__ to initialize instance members.

class Rectangle: def __init__(self, width, height): self.width = width self.height = height self.area = width * height # causes "Explicit return in __init__" error return self.area 

Best practices¶

Remove the return statement from the __init__ method¶

Remove the return statement in the __init__ method that is returning a value.

class Rectangle: def __init__(self, width, height): self.width = width self.height = height self.area = width * height # return statement removed from here 

Move the program logic to another instance method¶

There is no reason why the Rectangle class MUST return the area immediately upon initialization. This program logic should be moved to a separate method of the Rectangle class. The program can call the method later, after the object has successfully initialized.

class Rectangle(object): def __init__(self, width, height): self.width = width self.height = height self._area = width * height @property # moved the logic for returning area to a separate method def area(self): return self._area 

Note that the class must inherit from object now, since the property decorator only works for new style classes.

References¶

Источник

Оцените статью