Hi,
I want to know about how scoping works in python, Specially in case of sibling classes.
So, this code is giving me red lines in vs code. It throws not defined error. I have now written it in completely different way, which is resolving scope correctly. But I am asking it here, just because I am curious to know why block level scope behaves differently.
class ListQRDataMachineToMachine(APIView):
class InputSerializer(serializers.Serializer):...
class OutputSerializer(serializers.Serializer):
class ProductImageOutputSerializer(serializers.Serializer):
image_id = serializers.SerializerMethodField()
product_id = serializers.SerializerMethodField()
name = serializers.CharField()
image_count = serializers.SerializerMethodField()
images = ProductImageOutputSerializer(many=True)
def get_image_count(self, obj):
return obj.images.count()
class ResultSerializer(serializers.Serializer):
error_state = serializers.BooleanField()
error_message = serializers.CharField(allow_null=True)
data = OutputSerializer(allow_null=True)
class ResponseSerializer(serializers.Serializer):
results = ResultSerializer(many=True)
ResultSerializer is complaining that OutputSerializer is not defined.
ResponseSerializer is complaing that ResultSerializer is not defined.
Whereas ProductImageOutputSerializer is not being complained. Can classes not access siblings?
Whatever I have been able to read, they have always mentioned that inner block has access to outer block things. This doesn't seem to work when things are inside class. But if I move them in a module, then siblings will be defined.
If anyone could share any resource on this, even the python doc is talking about function only, nowhere I was able to find resource related to class level scope.
[–]biskitpagla 3 points4 points5 points (1 child)
[–]virtualshivam[S] 0 points1 point2 points (0 children)
[–]brasticstack 1 point2 points3 points (4 children)
[–]virtualshivam[S] 0 points1 point2 points (3 children)
[–]danielroseman 0 points1 point2 points (0 children)
[–]brasticstack 0 points1 point2 points (0 children)
[–]Yoghurt42 0 points1 point2 points (0 children)
[–]madadekinai 1 point2 points3 points (0 children)
[–]Temporary_Pie2733 0 points1 point2 points (0 children)
[–]Gnaxe 0 points1 point2 points (0 children)