Creating a Class Attribute dependent on Another Attribute - python-2.7

I am currently working on a small RPG in Pygame to get used to object oriented coding.
When looking into how to auto-update a property I came across the following:
class P:
def __init__(self,x):
self.x = x
#property
def x(self):
return self.__x
#x.setter
def x(self, x):
if x < 0:
self.__x = 0
elif x > 1000:
self.__x = 1000
else:
self.__x = x
I tried applying it to my code but I get the following error:
File "weapons.py", line 13, in __init__
self.name = '{} {}'.format(ammo, self.raw_name)
TypeError: name() takes exactly 3 arguments (2 given)
I understand what the error is but I don't get how to solve it since I need both the raw_name and the ammo attributes to auto-update my Arrow instance's name.
My class is as such:
class Projectile(Item):
def __init__(self, name, value, image, x, y, speed, dmg, dmg_modif, ammo):
super(Projectile, self).__init__(name, value, image, x, y)
self.dest = (self.rect[0],self.rect[1])
self.speed = speed
self.dmg_modif = dmg_modif
self.dmg = dmg
self.orientation = 0
self.ammo = ammo
#property
def name(self):
return self.___name
#name.setter
def name(self, raw_name, ammo):
if '{} {}'.format(raw_name,ammo) != self.___name:
self.___name = '{} {}'.format(raw_name,ammo)
The child class which returns the error is:
class Arrow(Projectile):
def __init__(self, ammo): #name, value, image, x, y, dmg
self.raw_name = 'Arrows'
self.name = '{} {}'.format(ammo, self.raw_name)
self.value = 5
self.image = variables.quiver_img
self.speed = 4
self.dmg = 2
self.dmg_modif = 1
super(Arrow, self).__init__(self.name, self.value, self.image, 200, 150, self.speed, self.dmg, self.dmg_modif, ammo)
And the parent classes are, Item and MySprite:
class Item(MySprite):
def __init__(self, name, value, image, x, y):
# Call the parent class (Sprite) constructor
super(Item, self).__init__(image, x, y)
self.name = name
self.value = value
self.inv_pos = -1
and
class MySprite(pygame.sprite.Sprite):
def __init__(self,image,x,y):
# Call the parent class (Sprite) constructor
super(MySprite, self).__init__()
self.image = image
self.rect = self.image.get_rect().move(x, y) #initial placement
self.top_cp = (self.rect[0]+self.rect[2]/2,self.rect[1])
self.bot_cp = (self.top_cp[0],self.rect[1]+self.rect[3])
self.left_cp = (self.rect[0],self.rect[1]+self.rect[3]/2)
self.right_cp = (self.left_cp[0]+self.rect[2],self.left_cp[1])
self.center = self.rect.center
self.pos = self.rect.topleft
self.blit_order = 1
self.level = variables.current_level #Level(1)#level to which sprite belongs
Any help would be welcome !

Please provide a minimal example next time.
Your code looks a little complicated. OK, the first problem is that you can't pass multiple arguments to a setter. You can either pass a tuple or just use a traditional setter method def set_name(self, name, ammo):.
Another problem is that you use the ___name attribute before it has been set, for example in the second line of the Arrows __init__ method.
Private attributes should have one underscore not three (that's just a convention to warn other programmers). If you have two or more underscores than the name gets mangled.
Also, it looks to me like you change the name in the setter only if the new name is equal to the old name (kinda pointless ;)). What do you actually want to do there? Maybe you don't need properties at all.
Here's a fixed (minimal) version of your code:
import pygame
class MySprite(pygame.sprite.Sprite):
def __init__(self):
super(MySprite, self).__init__()
class Item(MySprite):
def __init__(self, name):
super(Item, self).__init__()
self.name = name
class Projectile(Item):
def __init__(self, name):
super(Projectile, self).__init__(name)
#property
def name(self):
return self._name
#name.setter
def name(self, name):
self._name = name + ' foo'
class Arrow(Projectile):
def __init__(self):
super(Arrow, self).__init__(name='Arrows')
arrow = Arrow()
print(arrow.name)
arrow.name = 'New name'
print(arrow.name)

So I eventually managed to solve my problem:
class Projectile(object):
def __init__(self, raw_name, ammo):
self.raw_name = raw_name
self.name = self.raw_name
self.ammo = ammo
#property
def ammo(self):
return self._ammo
#ammo.setter
def ammo(self, ammo):
self.name = str(ammo) + self.raw_name
self._ammo = ammo
class Arrow(Projectile):
def __init__(self):
self.raw_name = ' Arrows'
self.name = self.raw_name
super(Arrow, self).__init__(self.raw_name, ammo
= 10)
arrow = Arrow()
print arrow.ammo
print arrow.name
arrow.ammo = 15
print arrow.ammo
print arrow.name
gives:
>>>10
>>>10 Arrows
>>>15
>>>15 Arrows

Related

How to convert a models.IntegerField() instance to int?

I am working on Django 4.0.2 in python 3.10.2
I've read How to convert a models.IntegerField() to an integer(the poster actually need copy-constructor function).And I've searched for Google.
But it doesn't help.
what I want to do is:
#In app/models.py
class Foo:
a1 = models.IntergeField()
a2 = models.IntergeField()
#....
#many else
b1 = convertToInt(a1) * 3 + convertToInt(a2) *4 + convertToInt(a7) #calc by needing
b2 = convertToInt(a2) * 2 + convertToInt(a3) + convertToInt(a5) #calc by needing
#....
#many else
#b(b is price actually) will be used in somewhere else.Its type need be int for programmer-friendly using
any advice?
P.S. English is not my first language.Please forgive my syntax mistakes.
Edit 1:
if just a1 * 3, I will receive
TypeError: unsupported operand type(s) for *: 'IntegerField' and 'int'
And I'd like to explain why the solution in the above attached link is not work
the first answer use:
class Foo(models):
nombre_etudiant = models.IntergeField()
place_disponible =models.IntegerField(blank=True,null=True)
def __init__(self,*args,**kwargs):
super(Foo, self).__init__(*args, **kwargs)
if self.place_disponible is None:
self.place_disponible = self.nombre_etudiant
which I still can't mutiply the num by n. The code just do copying.I still can't get the value in int type.
the 2nd solution
class MyModel(models):
nombre_etudiant = models.IntergeField()
place_disponible =models.IntegerField()
def save(self, *args, **kwargs):
if not self.place_disponible:
self.place_disponible = int(nombre_etudiant)
super(Subject, self).save(*args, **kwargs)
self.place_disponible = int(nombre_etudiant) this will catch excepetion like TypeError: int() argument must be a string, a bytes-like object or a real number, not 'IntegerField'
Since you indicated in the comments that you don't need to store the derived attributes, I propose the following solution. Here the attributes are calculated every time and you can use them as you would use a1 and a2 attributes.
class Foo(models.Model):
a1 = models.IntegerField()
a2 = models.IntegerField()
#property
def b1(self):
return self.a1*3 + self.a2*4 # + ...
#property
def b2(self):
return self.a2*3 + self.a3 # + ...

my inherited class(boss) won't accept the 3 parameters the main class(employee) accepts

What is wrong with my code? when i create a new Boss class i can't seem to get all the parameters from employe to work.
months = 6
class Employee(object):
balance = 0
company_income = 10000000000000000
def __init__(self, first, last, pay):
self.first = first
self.last = last
self.pay = pay
self.tasks = {}
self.fullname = "{} {}".format(self.first, self.last)
self.email = "{}.{}#Otterstad.com".format(self.first, self.last)
def finishTask(self, task):
del self.tasks[task]
def getMonths(self):
print self.pay * months
class Manager(Employee):
def __init__(self, first, last, pay):
super().__init__(first, last, pay)
self.employees = []
self.pay += 10000
def addTask(self, task):
if len(self.tasks) == 0:
self.tasks[1] = task
else:
self.tasks[len(self.tasks)+1] = task
class Boss(Employee):
def __init__(self, first, last, pay):
super().__init__(first, last, pay)
self.employees = []
def changeSalary(self, new_salary):
self.pay = new_salary
def giveBonus(self, bonus, employee):
employee.balance += bonus
def addTask(self, task):
if len(self.tasks) == 0:
self.tasks[1] = task
else:
self.tasks[len(self.tasks)+1] = task
st1 = Boss("test", "test", 2000000000)
You are calling super in way that is correct for Python 3, not 2.
In Boss.__init__, change
super().__init__(first, last, pay)
to
super(Boss, self).__init__(first, last, pay).
Similarly, change the other calls to super throughout your code.

Object not iterable when assigning to a list

I am coding a 2048 game via pygame. below is the relevant section of my code:
class Data():
def __init__(self):
self.data = getnull()
self.score = 0
def updatesprites(self): # EXP
spritelist = [[],[],[],[]]
for count in range(4): # for row loop
for i in range(4): # per column loop
if self.data[count][i] != 0:
spritelist[count]+= newSprite(str(self.data[count] [i])+".png") # error occurs here
spritelist[count][i].move(15 + i*115, 15 + count*115)
showSprite(spritelist[count][i])
class newSprite(pygame.sprite.Sprite):
def __init__(self,filename):
pygame.sprite.Sprite.__init__(self)
self.images=[]
self.images.append(loadImage(filename))
self.image = pygame.Surface.copy(self.images[0])
self.currentImage = 0
self.rect=self.image.get_rect()
self.rect.topleft=(0,0)
self.mask = pygame.mask.from_surface(self.image)
self.angle = 0
def addImage(self, filename):
self.images.append(loadImage(filename))
def move(self,xpos,ypos,centre=False):
if centre:
self.rect.center = [xpos,ypos]
else:
self.rect.topleft = [xpos,ypos]
----------------Main-------------------
from functions import *
from config import *
from pygame_functions import *
import pygame
screenSize(475,475) # call screen init
gameboard = newSprite("game board.png") # createboard
showSprite(gameboard)
game = Data()
game.updatesprites()
while True:
pass
when game.updatesprites() is called, "newSprite object is not iterable" error is raised in function Data.updatesprites
+ concatenates lists and strings, and adds numbers.
What you are trying to do, is to add an element to a list.
This is done as follows:
li.append(element) # adds the element to the end of the list
Or in your case:
spritelist[count].append(newSprite(str(self.data[count][i]) + ".png"))
Another solution: You could create a new type, that lets you add elements the way you were trying to:
class UglyList(list):
def __iadd__(self, other):
self.append(other)
You'd need to change another line here:
spritelist = [UglyList() for i in range(4)]

Manipulating instances of a class change the whole class

So I'm struggling. I've seen quite a few posts on this topic, but after a couple hours struggling with the problem, I can't figure this out. Here are a few snippets of code I have. I'd like to be able to change one instance of the class without the other being changed as well, and I'm coming up short
class voter:
def __init__(self, iPositon, jPosition, affiliation):
self.affiliation = affiliation
self.iPositon = iPositon
self.jPosition = jPosition
class district:
def __init__(self, *listOfVotersPassed):
# super(district, self).__init__()
self.listOfVoters = []
for x in listOfVotersPassed:
self.listOfVoters.append(x)
class city:
def __init__(self, *listOfDistrictsPassed):
self.listOfDistricts = []
for x in listOfDistrictsPassed:
self.listOfDistricts.append(x)
def main():
# I have a list of class district that I pass to city()
startCity = city(*startCityList)
solutionCity = city(*solutionCityList)
print solutionCity.listOfDistricts[0].listOfVoters[0].affiliation # Democrat
print startCity.listOfDistricts[0].listOfVoters[0].affiliation # Democrat
solutionCity.listOfDistricts[0].listOfVoters[0].affiliation = "Republican"
print solutionCity.listOfDistricts[0].listOfVoters[0].affiliation # Republican
print startCity.listOfDistricts[0].listOfVoters[0].affiliation # Republican
if __name__ == "__main__":
main()
edit.
So I was asked about how I created the instance(s) of city. I have a file I'm reading in, each has either R or D in each line.
file = open(fileName)
# import contents
startVoterList = []
solutionVoterList = []
for line in file:
for house in line:
if " " in house:
pass
elif "\n" in house:
pass
else:
startVoterList.append(house)
solutionVoterList.append(house)
I also updated the classes, now each is in the following format. No changes besides the "(object)" after each class className.
class voter(object):
def __init__(self, iPositon, jPosition, affiliation):
self.affiliation = affiliation
self.iPositon = iPositon
self.jPosition = jPosition

How do I fix the part in __str__ so it would display the name of the instance within the class? Python 2X

How do I change the code so that it would return a is a square with side:5 and area:25 ?? when I call print a?
class Square():
def __init__(self, length):
self.length = length
def area(self):
return self.length ** 2
def __str__(self): #this is the part I don't know how to write
return "is a square with side:%s and area:%s." % (self.length, self.area())
a = Square(5)
print a
I guess you want to get the name of the variable that you asign with your class, right??
I don't know how to do that or is that is posible (or usefull in any way) because you can have have multiples variables pointing to the same object
for example in this
a=SomeObject()
b=a
c=a
d=c
and in this case how you will know which one you are using if all of them point to the same object?
A alternative is give your class a extra paremetrer with the name that you wish for you instance
class Square():
def __init__(self, length,name=""):
self.length = length
self.name = name
def area(self):
return self.length ** 2
def __str__(self): #this is the part I don't know how to write
return "%s is a square with side:%s and area:%s." % (self.name, self.length, self.area())
a = Square(5,'MySquare')
print a # MySquare is a square with side:5 and area:25.