Minus equal operator doesnt call property setter python - python

I have my code setup this way:
class Test():
def __init__(self):
self.offset = [0,0]
#property
def offset(self):
return self._offset
#offset.setter
def offset(self,offset):
print("set")
self._offset = offset
test = Test()
test.offset[1] -= 1
but the setter is being called only once even though I am changing my variable twice, anyone is able to help ?

test.offset[1] -= 1
This line of your code is calling the getter not the setter. You get the list from the test object and then you alter its contents.
Same as if you wrote:
v = test.offset # get the list
v[1] -= 1 # alter the contents of the list

Related

Class that tracks data of all its active instantiations?

I have a class Foo with its instances having a "balance" attribute. I'm designing it in such a way that Foo can track all the balances of its active instances. By active I mean instances that are currently assigned to a declared variable, of part of a List that is a declared variable.
a = Foo(50) # Track this
b = [ Foo(20) for _ in range(5) ] # Track this
Foo(20) # Not assigned to any variable. Do not track this.
Another feature of Foo is that is has an overloaded "add" operator, where you can add two Foo's balances together or add to a Foo's balance by adding it with an int or float.
Example:
x = Foo(200)
x = x + 50
y = x + Foo(30)
Here is my code so far:
from typing import List
class Foo:
foo_active_instances: List = []
def __init__(self, balance: float = 0):
Foo.foo_active_instances.append(self)
self.local_balance: float = balance
#property
def balance(self):
"""
The balance of only this instance.
"""
return self.local_balance
def __add__(self, addend):
"""
Overloading the add operator
so we can add Foo instances together.
We can also add more to a Foo's balance
by just passing a float/int
"""
if isinstance(addend, Foo):
return Foo(self.local_balance + addend.local_balance)
elif isinstance(addend, float | int):
return Foo(self.local_balance + addend)
#classmethod
#property
def global_balance(cls):
"""
Sum up balance of all active Foo instances.
"""
return sum([instance.balance for instance in Foo.foo_active_instances])
But my code has several issues. One problem is when I try to add a balance to an already existing instance, like:
x = Foo(200)
x = x + 50 # Problem: This instantiates another Foo with 200 balance.
y = Foo(100)
# Expected result is 350, because 250 + 100 = 350.
# Result is 550
# even though we just added 50 to x.
print(Foo.global_balance)
Another problem is replacing a Foo instance with None doesn't remove it from Foo.foo_active_instances.
k = Foo(125)
k = None
# Expected global balance is 0,
# but the balance of the now non-existing Foo still persists
# So result is 125.
print(Foo.global_balance)
I tried to make an internal method that loops through foo_active_instances and counts how many references an instance has. The method then pops the instance from foo_active_instance if it doesn't have enough. This is very inefficient because it's a loop and it's called each time a Foo instance is made and when the add operator is used.
How do I rethink my approach? Is there a design pattern just for this problem? I'm all out of ideas.
The weakref module is perfect for this design pattern. Instead of making foo_active_instances a list, you can make it a weakref.WeakSet. This way, when a Foo object's reference count falls to zero (e.g., because it wasn't bound to a variable), it will be automatically removed from the set.
class Foo:
foo_active_instances = weakref.WeakSet()
def __init__(self, balance: float = 0) -> None:
Foo.foo_active_instances.add(self)
...
In order to add Foo objects to a set, you'll have to make them hashable. Maybe something like
class Foo:
...
def __hash__(self) -> int:
return hash(self.local_balance)
You can use inspect to check if the __init__ or __add__ methods have been called as part of an assignment statement. Additionally, you can keep a default parameter in __init__ to prevent increasing your global sum by the value passed to it when creating a new Foo object from __add__:
import inspect, re
def from_assignment(frame):
return re.findall('[^\=]\=[^\=]', inspect.getframeinfo(frame).code_context[0])
class Foo:
global_balance = 0
def __init__(self, balance, block=False):
if not block and from_assignment(inspect.currentframe().f_back):
Foo.global_balance += balance
self.local_balance = balance
def __add__(self, obj):
if from_assignment(inspect.currentframe().f_back) and not hasattr(obj, 'local_balance'):
Foo.global_balance += obj
return Foo(getattr(obj, 'local_balance', obj), True)
a = Foo(50)
b = [Foo(20) for _ in range(5)]
Foo(20)
print(Foo.global_balance) #150
x = Foo(200)
x = x + 50
y = Foo(100)
print(Foo.global_balance) #350

Getting the value of an object instead of its location in memory

I'm studying OOP in Python and I'm making a game with 5 dices. I made the Dado object to represent my dices and the GeraDado object to actually create the Dado object. If I create an object such as d1 = GeraDado() I can print its value using the d1.valor() method but if I try to append its value to a list or dictionary it returns None. If I do print(d1) it returns the object's location in memmory and not the value. How can I make it return the d1 = GeraDado() value instead of using the d1.valor() method, that just prints the value on screen?
from random import randint
class Dado:
def __init__(self, valor):
self.__valor = valor
def valor(self):
print(self.__valor)
class GeraDado:
def __init__(self):
self.__dado = Dado(randint(1,6))
def dado(self):
self.__dado.valor()
d1 = GeraDado()
print(d1)
Your Dado.valor and GeraDado.dado methods only print the value of the die, they don't actually return it, instead, make them return the value using the return statement:
Dado.valor:
def valor(self):
return self.__valor
GeraDado.dado:
def dado(self):
return self.__dado.valor()
Two little suggestions:
class Dado:
# ...
def valor(self):
return self.__valor
# 1. return stuff!
# Don't fall in love with print. It's just a debugging side-effect
class GeraDado:
# ...
def dado(self):
return self.__dado.valor() # 1. again: return!
# 2. implement a __str__ method.
# This is your class's representation that is used by print
def __str__(self):
return str(self.dado())
>>> d1 = GeraDado()
>>> print(d1)
1

Python Object Oriented

I was kinda playing around with Object Oriented Programming in python and ran into an error i havent encountered before..:
class Main:
def __init__(self, a , b):
self.a = a
self.b = b
def even(self):
start = self.a
slut = self.b
while start <= slut:
if start % 2 == 0:
yield start
start += 1
def odd(self):
start = self.a
slut = self.b
while start <= slut:
if start % 2 != 0:
yield start
start += 1
def display():
evens = list(num.even())
odds = list(num.odd())
print(f"{evens}'\n'{odds}")
num = Main(20, 50)
Main.display()
Take a look at the last class method, where there shouldent be a 'self' as a parameter for the program to Work..Why is that? I thought every class method should include a 'self' as a parameter? The program wont work with it
There should be a self parameter, if it's intended to be an instance method, and you would get an error if you tried to use it as so, i.e., num.display().
However, you are calling it via the class, and Main.display simply returns the function itself, not an instance of method, so it works as-is.
Given that you use a specific instance of Main (namely, num) in the body, you should replace that with self:
def display(self):
evens = list(self.even())
odds = list(self.odd())
print(f"{evens}'\n'{odds}")
and invoke it with
num.display()

Is there a way to fix Name Error due to scope?

I have a function that creates a player object but when referencing the object, I get a NameError. I think it is happening due to local scope but global should fix it...
I just started out OOP and this code is working in the python shell but it is not working in script mode.
endl = lambda a: print("\n"*a)
class Score:
_tie = 0
def __init__(self):
self._name = ""
self._wins = 0
self._loses = 0
def get_name(self):
print
self._name = input().upper()
def inc_score(self, wlt):
if wlt=="w": self._wins += 1
elif wlt=="l": self._loses += 1
elif wlt=="t": _tie += 1
else: raise ValueError("Bad Input")
def player_num(): #Gets number of players
while True:
clear()
endl(10)
print("1 player or 2 players?")
endl(5)
pnum = input('Enter 1 or 2: '.rjust(55))
try:
assert int(pnum) == 1 or int(pnum) == 2
clear()
return int(pnum)
except:
print("\n\nPlease enter 1 or 2.")
def create_player(): #Creates players
global p1
p1 = Score()
yield 0 #stops here if there is only 1 player
global p2
p2 = Score()
def pr_(): #testing object
input(p1._wins)
input(p2._wins)
for i in range(player_num()):
create_player()
input(p1)
input(p1._wins())
pr_()
wherever I reference p1 I should get the required object attributes but I'm getting this error
Traceback (most recent call last):
File "G:/Python/TicTacTwo.py", line 83, in <module>
input(p1)
NameError: name 'p1' is not defined
Your issue is not with global but with the yield in create_player(), which turns the function into a generator.
What you could do:
Actually run through the generator, by executing list(create_player()) (not nice, but works).
But I suggest you re-design your code instead, e.g. by calling the method with the number of players:
def create_player(num): #Creates players
if num >= 1:
global p1
p1 = Score()
if num >= 2:
global p2
p2 = Score()
If you fix this issue, the next issues will be
1) input(p1) will print the string representation of p1 and the input will be lost, you probably want p1.get_name() instead.
2) input(p1._wins()) will raise TypeError: 'int' object is not callable
I will redesign the app to introduce really powerful python constructs that may help you when getting into OOP.
Your objects are going to represent players, then don't call them Score, call them Player.
Using _tie like that makes it a class variable, so the value is shared for all the players. With only two participants this may be true but this will come to hurt you when you try to extend to more players. Keep it as a instance variable.
I am a fan of __slots__. It is a class special variable that tells the instance variables what attributes they can have. This will prevent to insert new attributes by mistake and also improve the memory needed for each instance, you can remove this line and it will work but I suggest you leave it. __slots__ is any kind of iterable. Using tuples as they are inmutable is my recomendation.
Properties are also a really nice feature. They will act as instance attribute but allow you to specify how they behave when you get the value (a = instance.property), assign them a value (instance.property = value), or delete the value (del instance.property). Name seems to be a really nice fit for a property. The getter will just return the value stored in _name, the setter will remove the leading and trailing spaces and will capitalize the first letter of each word, and the deletter will set the default name again.
Using a single function to compute a result is not very descriptive. Let's do it with 3 functions.
The code could look like this:
# DEFAULT_NAME is a contant so that we only have to modify it here if we want another
# default name instead of having to change it in several places
DEFAULT_NAME = "Unknown"
class Player:
# ( and ) are not needed but I'll keep them for clarity
__slots__ = ("_name", "_wins", "_loses", "_ties")
# We give a default name in case none is provided when the instance is built
def __init__(self, name=DEFAULT_NAME):
self._name = name
self._wins = 0
self._loses = 0
self._ties = 0
# This is part of the name property, more specifically the getter and the documentation
#property
def name(self):
""" The name of the player """
return self._name
# This is the setter of the name property, it removes spaces with .strip() and
# capitalizes first letters of each word with .title()
#name.setter
def name(self, name):
self._name = name.strip().title()
# This is the last part, the deleter, that assigns the default name again
#name.deleter
def name(self):
self._name = DEFAULT_NAME
def won(self):
self._wins += 1
def lost(self):
self._loses += 1
def tied(self):
self._ties += 1
Now that's all we need for the player itself. The game should have a different class where the players are created.
class Game:
_min_players = 1
_max_players = 2
def __init__(self, players):
# Check that the number of players is correct
if not(self._min_players <= players <= self._max_players):
raise ValueError("Number of players is invalid")
self._players = []
for i in range(1, players+1):
self._players.append(Player(input("Insert player {}'s name: ".format(i))))
#property
def players(self):
# We return a copy of the list to avoid mutating the inner list
return self._players.copy()
Now the game would be created as follows:
def new_game():
return Game(int(input("How many players? ")))
After that you would create new methods for the game like playing matches that will call the players won, lost or tied method, etc.
I hope that some of the concepts introduced here are useful for you, like properties, slots, delegating object creation to the owner object, etc.

Where to change a value so it applies to all methods in python?

I am struggling with a problem that didn't seem to be a big deal in the beginning. I am creating aa class and a sub Subclass and whenever I want to change a value in subclass it should automatically update all the other methods in the class and subclass whenever I do that. Let me give you an example. The value that's supposed to be changed is shift in the change.shift(shift) method.
class Message(object):
'''
code that doesn't matter for this question
'''
def build_shift_dict(self, shift):
'''
code that doesn't matter for this question
'''
return dict
def apply_shift(self, shift):
'''
code that doesn't matter for this question
'''
return newMessage
class PlaintextMessage(Message):
def __init__(self, text, shift):
'''
code that doesn't matter for this question
'''
Message.__init__(self, text)
self.shift = shift
self.encrypting_dict = self.build_shift_dict(self.get_shift())
self.message_text_encrypted = self.apply_shift(self.get_shift())
def get_shift(self):
'''
Used to safely access self.shift outside of the class
Returns: self.shift
'''
return self.shift
def get_encrypting_dict(self):
return self.encrypting_dict
def get_message_text_encrypted(self):
return self.message_text_encrypted
def change_shift(self, shift):
assert 0 <= shift < 26
self.shift = shift
As you can see I need shift to be changed as well within apply_shift(self, shift) and build_shift_dict(self, shift) because the init method in PlaintextMessage(Message) calls does methods. How do I do that? When I call the get_shift(self) method I get the shift value, if I then call the change_shift(self, shift) method and call get_shift(self) again it shows me the updated shift value. So I tried to update self.encrypting_dict = self.build_shift_dict(self.get_shift()) and self.message_text_encrypted = self.apply_shift(self.get_shift()) with the get-Method instead of shift but that doesn't change anything. What am I missing here? Thanks!
In change_shift() just call those other two methods.
def change_shift(self, shift):
assert 0 <= shift < 26
self.shift = shift
self.encrypting_dict = self.build_shift_dict(self.shift)
self.message_text_encrypted = self.apply_shift(self.shift)
You could define build_shift_dict and apply_shift without an argument, other than self, and just use self.shift in those methods - you would still have to call them but you wouldn't have to pass them anything, and you couldn't mess it up by passing those methods an erroneous value from your program.
You could make access to shift a property/attribute using the #property decorator - you won't have to call it and you rid yourself of getter's and setter's - you would need to rename shift to something like _shift or __shift everywhere else in the program.
like:
class Foo:
def __init__(self, shift):
self._shift = shift
#property
def shift(self):
return self._shift
f = Foo(2)
Then you access it with f.shift.

Categories