I have a python function:
class MyClass:
my_class_variable: str = Optional[None]
#classmethod
def initialize(cls):
cls.my_class_variable = cls.some_function()
I plan to use it like:
x = MyClass.my_class_variable
How can I guarantee have my_class_variable to have initialized with a value, eg how can I force call initialize() ?
you could do something like :
def dec(cls):
cls.my_class_var = cls.some_func()
return cls
#dec
class MyClass:
my_class_var = ""
#classmethod
def some_func(cls):
return "Cool :)"
print(MyClass.my_class_var) --> Cool :)
Another option would be to use a metaprogramming, but as long as there is only one simple thing to do, I would use a decorator :)
I'm trying to create my own class that acts like a regular type, like this:
class CustomType:
def __init__(self, content):
self.content = content
def __str__(self):
return self.content
This means I can do something like this:
a = CustomType("hi there")
print(a) # 'hi there'
But the problem is that I have to use a = CustomType("hi there"). Is there a way I can do something like a = /hi there\ or a similar solution that lets me create my own syntactic sugar?
Thanks.
No. Python does not support creating new syntax.
Note: I would not suggest doing this.
You can't do that because the parser wouldn't know to look for it, but if you wanted something similar, you could create a custom class that returns a new instance of CustomType when divided by a string:
class CustomSyntax:
def __truediv__(self, value):
return CustomType(value)
Then if you had an instance of that class (let's call it c), you could divide it by a string any time you wanted an instance of CustomType:
a = c/'hi there'
b = c/'hello world'
However, that's a little weird, and you'd be best off sticking to your regular constructor.
Having trouble understanding the problem in my code, new to classes (generally python too, so sorry if I name things wrong). I receive this error:
I think my code is too long winded to include in here, so I made a simplified version to test the concept below.
The question is, how can I create a new self object "self4"? Which would then be available to other functions within the class. Currently I get this error.
AttributeError: 'className' object has no attribute 'self4'
class className(object):
def __init__(self, self1=1,self2=2,self3=3):
self.self1=self1
self.self2=self2
self.self3=self3
def evaluate(self, self5):
print className.func1(self) + className.func2(self)
self.self5=self5
print className.func1(self)
def func1(self):
return self.self1 + self.self5
def func2(self):
self.self4 = self.self1+self.self2+self.self3
return self.self4
filename tester.py
import tester.py
mst=tester.className()
mst.evaluate()
Edit:
Your code works fine!
What is the Problem?
I still think it is better to move self4 into the init!
Original
I think the most logical thing would be to define self4 on init:
class className(object):
def __init__(self, self1=1, self2=2, self3=3):
self.self1 = self1
self.self2 = self2
self.self3 = self3
self.self4 = None
#rest of class
If anyone still has this issue: you get this error when your indentation is goofed.To fix the asked question above, you just have to add a space before the last two functions definitions, that is;
class className(object):
def __init__(self, self1=1,self2=2,self3=3):
self.self1=self1
self.self2=self2
self.self3=self3
def evaluate(self, self5):
print className.func1(self) + className.func2(self)
self.self5=self5
print className.func1(self)
def func1(self):
return self.self1 + self.self5
def func2(self):
self.self4 = self.self1+self.self2+self.self3
return self.self4
just make sure they all have similar indentation, and you are good to go.
You should pass self4 in method.
OK, in C# we have something like:
public static string Destroy(this string s) {
return "";
}
So basically, when you have a string you can do:
str = "This is my string to be destroyed";
newstr = str.Destroy()
# instead of
newstr = Destroy(str)
Now this is cool because in my opinion it's more readable. Does Python have something similar? I mean instead of writing like this:
x = SomeClass()
div = x.getMyDiv()
span = x.FirstChild(x.FirstChild(div)) # so instead of this
I'd like to write:
span = div.FirstChild().FirstChild() # which is more readable to me
Any suggestion?
You can just modify the class directly, sometimes known as monkey patching.
def MyMethod(self):
return self + self
MyClass.MyMethod = MyMethod
del(MyMethod)#clean up namespace
I'm not 100% sure you can do this on a special class like str, but it's fine for your user-defined classes.
Update
You confirm in a comment my suspicion that this is not possible for a builtin like str. In which case I believe there is no analogue to C# extension methods for such classes.
Finally, the convenience of these methods, in both C# and Python, comes with an associated risk. Using these techniques can make code more complex to understand and maintain.
You can do what you have asked like the following:
def extension_method(self):
#do stuff
class.extension_method = extension_method
I would use the Adapter pattern here. So, let's say we have a Person class and in one specific place we would like to add some health-related methods.
from dataclasses import dataclass
#dataclass
class Person:
name: str
height: float # in meters
mass: float # in kg
class PersonMedicalAdapter:
person: Person
def __init__(self, person: Person):
self.person = person
def __getattr__(self, item):
return getattr(self.person, item)
def get_body_mass_index(self) -> float:
return self.person.mass / self.person.height ** 2
if __name__ == '__main__':
person = Person('John', height=1.7, mass=76)
person_adapter = PersonMedicalAdapter(person)
print(person_adapter.name) # Call to Person object field
print(person_adapter.get_body_mass_index()) # Call to wrapper object method
I consider it to be an easy-to-read, yet flexible and pythonic solution.
You can change the built-in classes by monkey-patching with the help of forbidden fruit
But installing forbidden fruit requires a C compiler and unrestricted environment so it probably will not work or needs hard effort to run on Google App Engine, Heroku, etc.
I changed the behaviour of unicode class in Python 2.7 for a Turkish i,I uppercase/lowercase problem by this library.
# -*- coding: utf8 -*-
# Redesigned by #guneysus
import __builtin__
from forbiddenfruit import curse
lcase_table = tuple(u'abcçdefgğhıijklmnoöprsştuüvyz')
ucase_table = tuple(u'ABCÇDEFGĞHIİJKLMNOÖPRSŞTUÜVYZ')
def upper(data):
data = data.replace('i',u'İ')
data = data.replace(u'ı',u'I')
result = ''
for char in data:
try:
char_index = lcase_table.index(char)
ucase_char = ucase_table[char_index]
except:
ucase_char = char
result += ucase_char
return result
curse(__builtin__.unicode, 'upper', upper)
class unicode_tr(unicode):
"""For Backward compatibility"""
def __init__(self, arg):
super(unicode_tr, self).__init__(*args, **kwargs)
if __name__ == '__main__':
print u'istanbul'.upper()
You can achieve this nicely with the following context manager that adds the method to the class or object inside the context block and removes it afterwards:
class extension_method:
def __init__(self, obj, method):
method_name = method.__name__
setattr(obj, method_name, method)
self.obj = obj
self.method_name = method_name
def __enter__(self):
return self.obj
def __exit__(self, type, value, traceback):
# remove this if you want to keep the extension method after context exit
delattr(self.obj, self.method_name)
Usage is as follows:
class C:
pass
def get_class_name(self):
return self.__class__.__name__
with extension_method(C, get_class_name):
assert hasattr(C, 'get_class_name') # the method is added to C
c = C()
print(c.get_class_name()) # prints 'C'
assert not hasattr(C, 'get_class_name') # the method is gone from C
I'd like to think that extension methods in C# are pretty much the same as normal method call where you pass the instance then arguments and stuff.
instance.method(*args, **kwargs)
method(instance, *args, **kwargs) # pretty much the same as above, I don't see much benefit of it getting implemented in python.
After a week, I have a solution that is closest to what I was seeking for. The solution consists of using getattr and __getattr__. Here is an example for those who are interested.
class myClass:
def __init__(self): pass
def __getattr__(self, attr):
try:
methodToCall = getattr(myClass, attr)
return methodToCall(myClass(), self)
except:
pass
def firstChild(self, node):
# bla bla bla
def lastChild(self, node):
# bla bla bla
x = myClass()
div = x.getMYDiv()
y = div.firstChild.lastChild
I haven't test this example, I just gave it to give an idea for who might be interested. Hope that helps.
C# implemented extension methods because it lacks first class functions, Python has them and it is the preferred method for "wrapping" common functionality across disparate classes in Python.
There are good reasons to believe Python will never have extension methods, simply look at the available built-ins:
len(o) calls o.__len__
iter(o) calls o.__iter__
next(o) calls o.next
format(o, s) calls o.__format__(s)
Basically, Python likes functions.
I have a few dozen classes. Here are two of them:
class Class_A(ClassABC):
def __init__(self):
super().__init__()
def from_B(self, b):
#do stuff
def from_C(self, c):
#do stuff
#...
def to_B(self):
rt = Class_B()
rt.from_A(self)
return rt
def to_C(self):
rt = Class_C()
rt.from_A(self)
return rt
#...
class Class_B(ClassABC):
def __init__(self):
super().__init__()
def from_A(self, a):
#do stuff
def from_C(self, c):
#do stuff
def to_A(self):
rt = Class_A()
rt.from_B(self)
return rt
def to_C(self):
rt = Class_C()
rt.from_B(self)
return rt
#...
#class Class_C, Class_D, Class_E, etc,
and here is the ABC:
class ClassABC(metaclass=abc.ABCMeta):
#abc.abstractmethod
def __init__(self):
#do stuff
The problem I have is that all the to_* methods in the subclasses follow the same exact pattern, and it becomes tedious to implement them. I would like to automatically generate them in the ClassABC if possible, but so far I have failed. I also tried creating a class decorater for the subclasses, but that didn't work either. I have, however, managed to auto generate the methods in each subclass using exec(), but I rather have the ABC generate them or use class decoraters. Is there a way to do this?
Note: all the classes are in their own separate module
First of all, your to_* methods aren't going to work because you need to explicitly include self at the beginning of the argument list to be able to use it in the method body. Secondly, I'd go with something akin to JBernardo's suggestion.
def to_class(self, cls):
rt = cls()
rt.from_class(self.__class__)
return rt
def from_class(self, cls):
#do stuff, with different things happening based on what class cls is; this may be a good place to use a dict of functions with the class or classname as keys