Error: "illegal target for variable Annotation" using "@Property"

Asked

Viewed 585 times

2

Like pygame does not have a function similar to the library SFML -> View, I am developing a "camera" format to scroll the screen and preserve the positions of objects within the general coordinate within the "world".

As a standard of pygame, use the rectangle.

To facilitate manipulation, I want to change the variables left and top obtained in the get_rect, adding camera offset to these variables.

Following this example by @jsbueno, I’m having a hard time:

I have a class class Sprite(pygame.sprite.Sprite) and inside it I take the rectangle of an image and store self.ret = self.imagem.get_rect(). So I want to return a different amount than self.ret.left every time this variable is accessed.

But in declaring @property and just below def ret.left(self): i get illegal target for variable annotation. What would be the correct sitaxe in this case?

import pygame

class Sprite(pygame.sprite.Sprite):
    def __init__(self, imagem, x, y):
        self.imagem = pygame.image.load('imagem.png')
        self.ret = self.imagem.get_rect()

        @property
        def ret.left(self): # aqui aparece o erro 

inserir a descrição da imagem aqui

  • What would that be self.ret within the class, between the two methods? Is it indented wrong or was it to be so? As for the error, ret.left shouldn’t be ret_left?

  • 1

    Names of funções or métodos do not allow use of the character ..

  • @Andersoncarloswoss, sorry, it was an edit error, I corrected. ret is an instance of the class pygame.Surface.Rect (https://www.pygame.org/docs/ref/surface.html#pygame.Surface.get_rect)

  • I’d like to know how I can interfere with reading the variable self.ret.left using @property?

  • I improved the original question at the beginning to clarify my goal.

1 answer

2


As I said in the nomination comment funções/métodos does not allow the use of the character ., so the syntax error. What you can do is with the method decorated with @property return the class instance pygame.Surface.Rect as follows:

import pygame

class Sprite(pygame.sprite.Sprite):

    def __init__(self, imagem, x, y):
        self.imagem = pygame.image.load('imagem.png')
        self._ret = self.imagem.get_rect()

    @property
    def ret(self):
        return self._ret

Allowing then to do self.ret.left, but which we believe has no meaning, since ret is already an instance attribute self.ret.left is already accessible. Unless you plan to implement a setter for this attribute, where it would then be necessary getter. An alternative would be:

import pygame

class Sprite(pygame.sprite.Sprite):

    def __init__(self, imagem, x, y):
        self.imagem = pygame.image.load('imagem.png')
        self.ret = self.imagem.get_rect()

    @property
    def ret_left(self):
        return self.ret.left

    @property
    def ret_right(self):
        return self.ret.right
  • I actually just learned the logic of @property. Your example was not working, but then I realized that the problem in your code is in the identation. The 2 @property of your example has to be aligned with the def __init__.

  • I hadn’t noticed the indentation. Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.