Here is my class code:
class Example:
def __init__(self):
self.parameter1 = 1
def standardFunction(self):
print("Hello")
Is it possible to initialise the Example class and make sure that every method that may be called for this particular instance will always point to the standardFunction?
For example:
ex1 = Example()
ex1.test1()
prints "Hello"
ex1.test2()
prints "Hello"
ex1.test3.test4()
prints "Hello"
ex1.test5().test6().IamBatman(42)
prints "Hello"
Basically, I would like to always have the Example.standardFunction() called, disregarding the string after the first "dot". Plus I don't know what's being put after the dot - it may be any string, int, float, or null.
Is it possible to achieve such behaviour with Python?
Yes we can achieve similar behavior in Python by overloading __getattr__. Like this,
class Example:
def __init__(self):
self.parameter1 = 1
def standardFunction(self):
print("Hello")
def __getattr__(self, name):
return self.standardFunction
Related
Trying to check if a method is from a class. It's as simple as:
class Foo:
def bar(self):
return
f = Foo()
ismethod(f.bar, Foo) # Should evaluate to true
Syntax like hasattr(Foo(), 'bar') works if you know the method name, and the same with 'bar' in dir(Foo()); howeveer, I need to be able to pass the method object itself as the argument, not like a string as shown here. In my scenario, I need to tell if a method—passed as an argument—is of a specific class.
In other words: How do I tell if an object is a method of a class, without knowing the name of the object?
You need inspect.ismethod:
import inspect
def just_func(a, b):
return a + b
class SomeClass:
def just_method(self, a, b, c):
return a * b + c
obj = SomeClass()
print(inspect.ismethod(just_func)) # False
print(inspect.ismethod(obj.just_method)) # True
UPD:
Oh sorry, you need to check if it belongs to a particular class, then use:
print('SomeClass' in obj.just_method.__qualname__) # True
print('SomeClass' in just_func.__qualname__) # False
Here's what the function you want might look like:
def ismethod(func, cls):
return cls.__name__ in func.__qualname__ and '.' in func.__qualname__
It actually looks like a duplicate of this.
I have two methods which take different number of arguments. Here are the two functions:
def jumpMX(self,IAS,list):
pass
def addMX(self,IAS):
pass
I am using a function which will return one of these functions to main.I have stored this returned function in a variable named operation.
Since the number of parameters are different for both,how do I identify which function has been returned?
if(operation == jumpMX):
operation(IAS,list)
elif(operation == addMX):
operation(IAS)
What is the syntax for this?Thanks in advance!
You can identify a function through its __name__ attribute:
def foo():
pass
print(foo.__name__)
>>> foo
...or in your case:
operation.__name__ #will return either "jumpMX" or "addMX" depending on what function is stored in operation
Here's a demo you can modify to your needs:
import random #used only for demo purposes
def jumpMX(self,IAS,list):
pass
def addMX(self,IAS):
pass
def FunctionThatWillReturnOneOrTheOtherOfTheTwoFunctionsAbove():
# This will randomly return either jumpMX()
# or addMX to simulate different scenarios
funcs = [jumpMX, addMX]
randomFunc = random.choice(funcs)
return randomFunc
operation = FunctionThatWillReturnOneOrTheOtherOfTheTwoFunctionsAbove()
name = operation.__name__
if(name == "jumpMX"):
operation(IAS,list)
elif(name == "addMX"):
operation(IAS)
You can import those functions and test for equality like with most objects in python.
classes.py
class MyClass:
#staticmethod
def jump(self, ias, _list):
pass
#staticmethod
def add(self, ias):
pass
main.py
from classes import MyClass
myclass_instance = MyClass()
operation = get_op() # your function that returns MyClass.jump or MyClass.add
if operation == MyClass.jump:
operation(myclass_instance, ias, _list)
elif operation == MyClass.add:
operation(myclass_instance, ias)
However, I must emphasize that I don't know what you're trying to accomplish and this seems like a terribly contrived way of doing something like this.
Also, your python code examples are not properly formatted. See the PEP-8 which proposes a standard style-guide for python.
I have a class where I want to call different modules form it, depending on an input from the user.
In my mind it would look something like this:
class Test:
def test1:
print("Hello world")
def test2:
print("farewell world")
user = input("> ")
Test.f{user}
Where it now will call what the user has told it to, but it doesn't work.
So my question is if it's possible, if it is then how I would accomplish it.
When trying examples from the given link, I keep encountering a problem for example where it tells me
"TypeError: test1() takes 0 positional arguments but 1 was given"
when the input looks like so:
getattr(globals()['Test'](), 'test')()
this is not the only one, and all I have tried leads to problems.
leading me to believe that either my problem is different, or I'm implementing it wrong.
Help with either scenario is much a appreciate.
You should try using getattr().
for example:
class Test:
def test1():
print("Hello world")
def test2():
print("farewell world")
userinput = input('Which function do you wish to call?\n')
# getattr() takes 2 parameters, class name, and attribute name.
myfunc = getattr(Test, userinput)
myfunc()
I'm playing around and testing data structure and noticed you could print a list inside a class or a def function if you do class.list[0] or def.list[0], so I tried seeing how deep it could go and adding func inside func inside the class but instead of my expectation to just add more dots to the end of the value to chain them, it seems it doesn't work past 1 value.
class player:
def __init__(self, name, spec):
self.name = name
self.spec = spec
var1 = ('A1','A2')
def def1():
defA = "printed defA"
def def2():
defB = ('B1','B2')
print(player.def1.def2.defB[0]) #Doesn't work---
print(player.var1[0]) #Works fine---
In this case, would there be a way to print (or anything else) to the values nested deep in there? What would the address of this value be?
Sorry if the title is confusing. I'm writing a minimalist game engine, and trying to define a class called "Area" where if the player enters the area, a function defined by the user happens. For example, one could create an instance
Area(location,function) that would fire function on the player when the player enters location (for the sake of simplicity, let it be a point or something).
Note: in pseudo-python
# in init.py
...
def function(player):
kill player
deathZone = Area(location,function)
--------------------------------------
# in player.update()
...
for area on screen:
if player in area:
Area.function(player)
The point of this is that the developer (aka me) can use any function they choose for the area. Is there anyway to do this, or should I try a better approach?
Sure, this kind of thing is certainly possible. In python, everything is an object, even a function. So you can pass around a function reference as a variable. For example try the following code:
import math
def rectangle(a, b):
return a*b
def circle(radius):
return math.pi * radius**2
class FunctionRunner(object):
def __init__(self):
self.userFunction = None
self.userParams = None
def setUserFunction(self, func, *params):
self.userFunction = func
self.userParams = params
def runFunction(self):
return self.userFunction(*self.userParams)
if __name__ == '__main__':
functionRunner = FunctionRunner()
functionRunner.setUserFunction(rectangle, 6, 7)
print(functionRunner.runFunction())
functionRunner.setUserFunction(circle, 42)
print(functionRunner.runFunction())
Here you have two functions that are defined for an area, and a class called FunctionRunner which can run any function with any number of input arguments. In the main program, notice that you need only pass the reference to the function name, and any input arguments needed to the setUserFunction method. This kind of thing will allow you to execute arbitrary code on the fly.
Alternatively, you could also replace a method on your class with a reference to another function (which is what you are asking), though this seems less safe to me. But it is certainly possible. For example you could have a class like this:
class FunctionRunner2(object):
def __init__(self):
pass
def setUserFunction(self, func):
self.theFunction = func
def theFunction(self, *params):
pass
And then do this:
if __name__ == '__main__':
functionRunner2 = FunctionRunner2()
functionRunner2.setUserFunction(rectangle)
print(functionRunner2.theFunction(6,7))
functionRunner2.setUserFunction(circle)
print(functionRunner2.theFunction(42))