I want to create monad that is able to handle mathematical errors gracefully. I created something like:
from enum import Enum
class Monad(Enum):
Safe = 1
Fail = 2
class DivisionMonad:
def __init__(self, value=None, type=Monad.Safe):
self.value = value
self.type = type
def __truediv__(self, val):
if self.type == Monad.Fail:
return DivisionMonad(type=Monad.Fail)
try:
return DivisionMonad(self.value / val)
except:
return DivisionMonad(type=Monad.Fail)
def __repr__(self):
return f'< {self.value} >'
d = DivisionMonad(1)
d = d / 3
print(d.value)
print(d.type)
d = d / 0
print(d.value)
print(d.type)
But I believe this could be simpler by making the class use different methods based on the state of the Monad. Is there a way to create conditional execution of methods based on an instance attribute other than if/else statements?
Related
Just a simple class definition withh subclasses to show inheritance
import datetime
class LibaryItem: #The base class definition
def __init__(self, t, a, i): # initialiser method
self.__Title = t
self.__Author_Artist = a
self.__ItemID = i
self.__OnLoan = False
self.DueDate = datetime.date.today()
def GetTitle(self):
return(self.__Title)
# All other Get methods go here
def Borrowing(self):
self.__OnLoan = True
self.__DueDate = self.__DueDate + datetime.timedelta(weeks = 3)
def Returning(self):
self.OnLoan = False
def PrintDetails(self):
print(self.__Title, '; ', self.__Author_Artist,'; ',end='') # end='' Appends a space instead of a newline
print(self.__ItemID, '; ', self.__OnLoan,'; ', self.__DueDate)
class Book(LibaryItem):# A subclass definition
def __init__(self, t, a, i): # Initialiser method
LibaryItem.__init__(self, t, a, i)
# This statement calls the constructor for the base class
self.__IsRequested = False
self.__RequestBy = 0
def GetIsRequested(self):
return(self.__IsRequested)
class CD(LibaryItem):
def __init__(self, t, a, i): # Initialiser method
LibaryItem.__init__(self, t, a, i)
self.__Genre = ""
def GetGenre(self):
return(self.__Genre)
def SetGenre(self, g):
self.__Genre = g
Instantiating a subclass
ThisBook = Book('Title', 'Author', 'ItemID')
ThisCD = CD('Title', 'Author', 'ItemID')
This is my problem here I don't understand why the ThisBook the object's attribute doesn't change from False its default value to True.
# Using A method
print(ThisBook.GetIsRequested())
ThisBook.IsRequested = True
print(ThisBook.GetIsRequested())
Thank you a reason to why this doesn't work would be helpful
You probably meant to do
ThisBook.__IsRequested = True
which you can't do because of name mangling. You could write another setter.
But before you dive too deeply into writing a lot of getters and setters you should be aware that the pythonic way is to not use them. Or, if additional logic is required, to use the #property decorator.
class LibaryItem:
def __init__(self, title, author, itemid): # initialiser method
self.title = title
self.author = author
self.itemid = itemid
self._onloan = False
self.duedate = datetime.date.today()
#property
def onloan(self):
return self._onloan
#onloan.setter
def onloan(self, value):
if value:
self.duedate += datetime.timedelta(weeks = 3)
self._onloan = value
def __str__(self):
return "%s; %s; %s; %s; %s" % (self.title, self.author, self.itemid, self.onloan, self.duedate)
class Book(LibaryItem):
def __init__(self, title, author, itemid):
LibaryItem.__init__(self, title, author, itemid)
self.requested = False
self.requestby = 0
and then
ThisBook = Book('Title', 'Author', 'ItemID')
print(ThisBook.requested)
ThisBook.requested = True
ThisBook.onloan = True
print(ThisBook.duedate)
You can't access a field with 2 underscores prefix like that (see What is the meaning of a single- and a double-underscore before an object name?).
You need to write a proper setter:
def SetIsRequested(self, val):
self.__IsRequested = val
What you are experiencing is the typical silliness of dynamic languages. A field on class can be set w/o being declared and the interpreter can't help you by pointing out that you've just created a new field called "IsRequested" in your class. Saves you some typing but costs you in ability of your interpreter and IDE to prevent you from messing up.
Is it possible to initialize a static/class dictionary in an inner enum class like other variables?
# file Outer.py
from enum import Enum
class Outer:
def callInner(self):
all_a = Outer.Inner.ALL
print(all_a) # prints Inner.ALL instead of the list
all_b = Outer.Inner.ALL[:] # TypeError Inner is not subscriptable
for e in all_a: #Inner is not iterable
print(e.to_string())
class Inner(Enum):
A = 1
B = 2
ALL = [A,B]
NAMES = {A : "some_name_other_than_enum_a_name",
B : "some_name_other_than_enum_b_name"}
def to_string(self):
return Outer.Inner.NAMES[self.value]
if __name__ == '__main__':
o = Outer()
o.callInner()
The class Outer is the module with all the logic. The class Inner is an enum which does contain the enum key-value pairs A=1and B=2 as well as a list of all possible enums (or any interesting subset thereof). The idea is to have a list for quick reference and iteration/enumeration over those while to_string could be an arbritray method containing any logic. The name lookup is just a simplification to make the problem clear.
The issue here is not that you have an inner class, but that the inner class is an Enum; however, it is possible to have non-member attributes as part of an Enum class -- see this question and answer for the details.
To summarize, you need to make ALL and NAMES with some kind of descriptor to avoid having them transformed into enum members:
# inherit from `object` if using Python 2
class classattribute: # was called Constant in the linked answer
def __init__(self, value):
self.value = value
def __get__(self, *args):
return self.value
def __set__(self, _, value):
self.value = value
def __repr__(self):
return '%s(%r)' % (self.__class__.__name__, self.value)
and then in your Inner:
ALL = classattribute([A, B])
NAMES = classattribute({
A : "some_name_other_than_enum_a_name",
B : "some_name_other_than_enum_b_name",
})
This will avoid the errors you are getting in your callInner() method, but will add a new one at the print(e.to_string()) line:
AttributeError: 'int' object has no attribute 'to_string'
The reason for this is that constructing an Enum is a two-part process:
gather all the definitions:
{
'A':1,
'B':2,
'ALL':classattribute([A, B]),
'NAMES':classattribute({'A':..., 'B':...}),
'to_string':method(...),
}
transform anything not a __dunder__, _sunder_, nor descriptor into an enum member:
A -> <Inner.A: 1>
B -> <Inner.B: 2>
What this means is that when ALL and NAMES were being created, A and B were still ints, and ints don't have a to_string method. The easy way around that is to retrieve the enum member before trying to access those methods: self.Inner(e).to_string().
To pull it all together, here is what your code should look like:
# file Outer.py
from enum import Enum
class classattribute:
def __init__(self, value):
self.value = value
def __get__(self, *args):
return self.value
def __repr__(self):
return '%s(%r)' % (self.__class__.__name__, self.value)
class Outer:
def callInner(self):
all_a = Outer.Inner.ALL
print(all_a)
all_b = Outer.Inner.ALL[:]
for e in all_a: #Inner is not iterable
print(self.Inner(e).to_string())
class Inner(Enum):
A = 1
B = 2
ALL = classattribute([A,B])
NAMES = classattribute(
{A : "some_name_other_than_enum_a_name",
B : "some_name_other_than_enum_b_name"}
)
def to_string(self):
return Outer.Inner.NAMES[self.value]
if __name__ == '__main__':
o = Outer()
o.callInner()
and when run, this is what you get:
[1, 2]
some_name_other_than_enum_a_name
some_name_other_than_enum_b_name
I am trying to dynamically create classes in Python and am relatively new to classes and class inheritance. Basically I want my final object to have different types of history depending on different needs. I have a solution but I feel there must be a better way. I dreamed up something like this.
class A:
def __init__(self):
self.history={}
def do_something():
pass
class B:
def __init__(self):
self.history=[]
def do_something_else():
pass
class C(A,B):
def __init__(self, a=False, b=False):
if a:
A.__init__(self)
elif b:
B.__init__(self)
use1 = C(a=True)
use2 = C(b=True)
You probably don't really need that, and this is probably an XY problem, but those happen regularly when you are learning a language. You should be aware that you typically don't need to build huge class hierarchies with Python like you do with some other languages. Python employs "duck typing" -- if a class has the method you want to use, just call it!
Also, by the time __init__ is called, the instance already exists. You can't (easily) change it out for a different instance at that time (though, really, anything is possible).
if you really want to be able to instantiate a class and receive what are essentially instances of completely different objects depending on what you passed to the constructor, the simple, straightforward thing to do is use a function that returns instances of different classes.
However, for completeness, you should know that classes can define a __new__ method, which gets called before __init__. This method can return an instance of the class, or an instance of a completely different class, or whatever the heck it wants. So, for example, you can do this:
class A(object):
def __init__(self):
self.history={}
def do_something(self):
print("Class A doing something", self.history)
class B(object):
def __init__(self):
self.history=[]
def do_something_else(self):
print("Class B doing something", self.history)
class C(object):
def __new__(cls, a=False, b=False):
if a:
return A()
elif b:
return B()
use1 = C(a=True)
use2 = C(b=True)
use3 = C()
use1.do_something()
use2.do_something_else()
print (use3 is None)
This works with either Python 2 or 3. With 3 it returns:
Class A doing something {}
Class B doing something []
True
I'm assuming that for some reason you can't change A and B, and you need the functionality of both.
Maybe what you need are two different classes:
class CAB(A, B):
'''uses A's __init__'''
class CBA(B, A):
'''uses B's __init__'''
use1 = CAB()
use2 = CBA()
The goal is to dynamically create a class.
I don't really recommend dynamically creating a class. You can use a function to do this, and you can easily do things like pickle the instances because they're available in the global namespace of the module:
def make_C(a=False, b=False):
if a:
return CAB()
elif b:
return CBA()
But if you insist on "dynamically creating the class"
def make_C(a=False, b=False):
if a:
return type('C', (A, B), {})()
elif b:
return type('C', (B, A), {})()
And usage either way is:
use1 = make_C(a=True)
use2 = make_C(b=True)
I was thinking about the very same thing and came up with a helper method for returning a class inheriting from the type provided as an argument.
The helper function defines and returns the class, which is inheriting from the type provided as an argument.
The solution presented itself when I was working on a named value class. I wanted a value, that could have its own name, but that could behave as a regular variable. The idea could be implemented mostly for debugging processes, I think. Here is the code:
def getValueClass(thetype):
"""Helper function for getting the `Value` class
Getting the named value class, based on `thetype`.
"""
# if thetype not in (int, float, complex): # if needed
# raise TypeError("The type is not numeric.")
class Value(thetype):
__text_signature__ = "(value, name: str = "")"
__doc__ = f"A named value of type `{thetype.__name__}`"
def __init__(self, value, name: str = ""):
"""Value(value, name) -- a named value"""
self._name = name
def __new__(cls, value, name: str = ""):
instance = super().__new__(cls, value)
return instance
def __repr__(self):
return f"{super().__repr__()}"
def __str__(self):
return f"{self._name} = {super().__str__()}"
return Value
Some examples:
IValue = getValueClass(int)
FValue = getValueClass(float)
CValue = getValueClass(complex)
iv = IValue(3, "iv")
print(f"{iv!r}")
print(iv)
print()
fv = FValue(4.5, "fv")
print(f"{fv!r}")
print(fv)
print()
cv = CValue(7 + 11j, "cv")
print(f"{cv!r}")
print(cv)
print()
print(f"{iv + fv + cv = }")
The output:
3
iv = 3
4.5
fv = 4.5
(7+11j)
cv = (7+11j)
iv + fv + cv = (14.5+11j)
When working in IDLE, the variables seem to behave as built-in types, except when printing:
>>> vi = IValue(4, "vi")
>>> vi
4
>>> print(vi)
vi = 4
>>> vf = FValue(3.5, 'vf')
>>> vf
3.5
>>> vf + vi
7.5
>>>
class Books():
def __init__(self):
self.__dict__['referTable'] = 1
#property
def referTable(self):
return 2
book = Books()
print(book.referTable)
print(book.__dict__['referTable'])
Running:
vic#ubuntu:~/Desktop$ python3 test.py
2
1
Books.referTable being a data descriptor is not shadowed by book.__dict__['referTable']:
The property() function is implemented as a data descriptor.
Accordingly, instances cannot override the behavior of a property.
To shadow it, instead of property built-in descriptor i must use my own descriptor. Is there a built in descriptor like property but which is non-data?
To expand on my comment, why not simply something like this:
>>> class Books():
... def __init__(self):
... self.__dict__['referTable'] = 1
... #property
... def referTable(self):
... try:
... return self.__dict__['referTable']
... except KeyError:
... return 2
...
>>> a = Books()
>>> a.referTable
1
>>> del a.__dict__['referTable']
>>> a.referTable
2
Now, I'd like to note that I don't think this is good design, and you'd be much better off using a private variable rather than accessing __dict__ directly. E.g:
class Books():
def __init__(self):
self._referTable = 1
#property
def referTable(self):
return self._referTable if self._referTable else 2
In short, the answer is no, there is no alternative to property() that works in the way you want in the Python standard library.
There is something very similar to a built-in non-data descriptor -- the class attribute:
class Books():
referTable = 'default'
def __init__(self, referTable=None):
if referTable is not None:
self.referTable = referTable
book = Books()
print(book.referTable)
# default
book.referTable = 'something specific'
print(book.referTable)
# something specific
If you need something more like a property (for example, you want a function to do some heavy-lifting the first time, but then use that first value for all future references), then you will need to build it yourself:
class OneTime(object):
def __init__(self, method):
self.name = method.__name__
self.method = method
def __get__(self, inst, cls):
if inst is None:
return self
result = self.method(inst)
inst.__dict__[self.name] = result
return result
class Books(object):
#OneTime
def referTable(self):
print 'calculating'
return 1 * 2 * 3 * 4 * 5
b = Books()
print b.__dict__
print b.referTable
print b.__dict__
print b.referTable
With the following results:
{}
calculating
120
{'referTable': 120}
120
Imagine the following class that displays some sort of hierarchy:
class BaseList2D(object):
def __init__(self):
self._superobject = None
self._subobjects = []
def InsertUnder(self, other):
if self not in other._subobjects:
other._subobjects.append(self)
self._superobject = other
return True
return False
def InsertAfter(self, other):
parent = other._superobject
if not parent:
return False
parent = parent._subobjects
parent.insert(parent.index(other) + 1, self)
return True
def GetDown(self):
if not len(self._subobjects):
return
return self._subobjects[0]
def GetNext(self):
if not self._superobject:
return
stree = self._superobject._subobjects
index = stree.index(self)
if index + 1 >= len(stree):
return
return stree[index + 1]
Is it really the best (or the only) way to set the superobject of other by accessing it's hidden attribute ? The attribute should not be set by the user ..
_foo is just a naming convention. Usually, there would be a property or something that sets the 'private' variable for you. If not, the convention is being (slightly) misused.