Найти - Пользователи
Полная версия: super() вопрос [python3]
Начало » Python для новичков » super() вопрос [python3]
1
KULINAR847
   
class A:
	def __new__(cls, *args):							
		if (len(args) == 0):
			return super(A, cls).__new__(cls)
		
		if int(args[0]) < 5:
			return super(A, cls).__new__(cls)
		else:
			return 0
	def __init__(self, n):
		n = n + 1
		print('a ' + str(n))
		B(n)
	
class B(A):
	def __new__(cls, *args):							
		return super(B, cls).__new__(cls)
	
	def __init__(self, n):
		n = n + 1
		print('b ' + str(n))
		try:
			# Почему если раскомментировать эту строку код выполняется - b 6
			#A(n)
			# А если раскомментировать эту строку то ошибка ? - b 664error 
			#super().__init__(n)
		except:
			print('error')
a = A(0)
PEHDOM
http://python.su/forum/post/204427/
AD0DE412
 instances = []
class A:
    def __new__(cls, *args):
        instances.append(cls)
        print(len(instances))
        if len(args) == 0:
            return super(A, cls).__new__(cls)
        if int(args[0]) < 5:
            return super(A, cls).__new__(cls)
        else:
            return 0
    def __init__(self, n):
        n = n + 1
        print('a ' + str(n))
        B(n)
class B(A):
    def __new__(cls, *args):
        return super(B, cls).__new__(cls)
    def __init__(self, n):
        n = n + 1
        print('b ' + str(n))
        try:
#             Почему если раскомментировать эту строку код выполняется - b 6
#            A(n)
#            if int(args[0]) < 5:
#             А если раскомментировать эту строку то ошибка ? - b 664error
#            super().__init__(n)
#            тк бесконечная рекурсия  
        except Exception as err:
            print(err)
a = A(0)
зы уже ответили в другой ветке
ззы поправте если ошибаюсь super().__init__(n) в классе В дергает только метод из класа А без создания энкземпляра класса А
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB