How I can call a function inside class like this code? I tried, but I can not... The idea is: I want update the other function inside de class, than I use this code, without sucess: app=MyApp() app.build.last1.text=time.strftime("%H:%M:%S \n")
How I do it?
(...)
class PL1_detect(Button):
def update(self, dt):
global ModoPisca
global t
global ModoPisca2
global last2
#ModoPisca=0
def tpisca():
conta=0
global estado_PTT1
##clock(self, dt)
app=MyApp()
app.build.last1.text=time.strftime("%H:%M:%S \n")
#(here the problem: HOW I CAN CALL FUNCTION OF CLASS
MYAPP(APP) ????
(...)
class MyApp(App):
def build(self):
layout = FloatLayout(size=(800, 600))
# Make the background gray:
with layout.canvas.before:
Color(.2,.2,.2,1)
self.rect = Rectangle(size=(800,600), pos=layout.pos)
wimg = Image(source='logo.png', pos=(0,180))
last1=Label(text=(" "), pos=(-330, -110), font_size='17sp', bold=0)
return layout
if __name__ == '__main__':
MyApp().run()
You can create object for that class
M=Myapp()
M.build() will help in calling that function.
Try this for your scenario
class A():
def funct1(self):
sample="hi"
class B():
def func2(self):
A.sample="hello"
print A.sample
a=A()
a.funct1()
b=B()
b.func2()
Output:
hello
Related
the code:
class ceshi():
def one(self):
global a
a = "i m a"
def two(self):
print(a)
if __name__ == '__main__':
ceshi().two()
error message:
NameError: name 'a' is not defined
Didn't I define "a"? why the error message is 'name "a" is not defind'
You never actually define a. Just because you have it within function one does not mean that this function will be called.
You should either move a out of the class scope:
a = "i m a"
class ceshi():
def one(self):
# some other code
def two(self):
print(a)
or make a call to one() before calling two(), in order to define a.
if __name__ == '__main__':
ceshi().one()
ceshi().two()
"a" is only defined inside def one(), so you should declare it outside the method scope and later on define its value inside def one() if that's what you want to do. You could just leave it inside def one() but if the method isn't called, it still won't work.
So your code should look like:
class ceshi():
global a
def one(self):
a = "i m a"
def two(self):
print(a)
class TestGlobal():
def one(self):
global a
a = "i m a"
def two(self):
global a
print(a)
or you can write this way
class TestGlobal():
global a
def __init__(self):
self.a = self.one()
def one(self):
self.a = "i m a 5"
def two(self):
print('------------------>',self.a)
when you defining any variable in class and method then it consider for particular class or method. So if it is global variable then call it as global first then used it.
I want it so that when the active() function Is called in the StopSign class, it calls the stop() function in the Car class. However, in my actual code(code is kinda long and distractingly sloppy so I used this one so you can get the idea) when I call upon the active function StopSign object has no attribute 'movement_code'. How can I get around this?
class Car:
def __init__(self):
self.color = blue
more code
def go(self):
self.movement_code = movement_function
def stop:
self.movement_code.kill()
class StopSign(car):
def __init__(self):
some code
def active(self):
self.stop()
The problem you're having is that StopSign wasn't initializing its parent class and so didn't inherit the stop method. Besides that though I agree with Arya. It doesn't make sense to have the StopSign class inherit from the Car class.
class Car:
def __init__(self):
self.color = 'blue'
# more code
def go(self):
pass
# self.movement_code = movement_function
def stop(self):
pass
# self.movement_code.kill()
class StopSign(Car):
def __init__(self):
super().__init__()
pass
# some code
def active(self):
self.stop()
sign = StopSign()
sign.active()
Here is a simple example of how you could avoid to have StopSign subclass Car:
class Car:
def __init__(self):
self.color = 'blue'
def go(self):
pass
def stop(self):
pass
class StopSign:
def __init__(self):
self.cars_in_vicinity = []
def active(self):
for car in self.cars_in_vicinity:
car.stop()
car = Car()
sign = StopSign()
sign.cars_in_vicinity.append(car)
sign.active()
There are many other ways to approach this though.
I suppose I'm missing something obvious, but I can't get the name of methods when I'm using decorators. When I run this code, I get the error:
AttributeError: 'str' object has no attribute "__name__"
Could somebody tell me how I can get the name of these decorated method?
Thanks
def Print(*arg, **kwarg):
func, *arguments = arg
print(func.__name__ + "(): {}".format(func=arguments[0]))
class Bob(object):
def __init__(self):
pass
#property
def stuff(self):
return "value from stuff property"
#stuff.setter
def stuff(self, noise):
return noise
class Tester:
def __init__(self):
self.dylan = Bob()
def randomTest(self):
Print(self.dylan.stuff, 1)
if __name__ == "__main__":
whatever = Tester()
whatever.randomTest()
stuff isn't a function or a method; it's a property. The syntax
#property
def stuff(...):
...
creates an instance of the property class using stuff as the argument to property, equivalent to
def stuff(...):
....
stuff = property(stuff)
and instances of property don't have a __name__ attribute, as you've seen.
(It's a little trickier with the setter, since the function and the property have to have the same name. But defining stuff a "second" time doesn't override the existing property named stuff.)
The individual methods are accessed via attributes of the property.
>>> Bob.stuff.fget.__name__
'stuff'
>>> Bob.stuff.fset.__name__
'stuff'
Note another, longer, way to create the same property:
class Bob:
def stuff_getter(self):
...
def stuff_setter(self, noise):
...
stuff = property(stuff_getter, stuff_setter)
del stuff_getter, stuff_setter # Clean up the namespace
def Print(*arg, **kwarg):
func, *arguments = arg
print(func.__name__ + "(): {}".format(func=arguments[0]))
class Bob():
def __init__(self, s):
self.stuff = s
#property
def myStuff(self):
return self.stuff
#myStuff.setter
def setStuff(self, noise):
self.stuff = noise
class Tester:
def __init__(self):
self.dylan = Bob(1)
def randomTest(self):
print(self.dylan.stuff)
if __name__ == "__main__":
whatever = Tester()
whatever.randomTest()
This should work :)
I am new to OOP and I am wondering why I need to supply the parameter MainWindow to the final line. If I call outside_func by itself, I don't need a parameter, but when I call it within a class i need to supply the class name for it to work. For example, MainWindow.class_func2() throws an error
class MainWindow():
def __init__(self):
print("in init")
def claas_func(self):
print ("func1")
def class_func2(self):
outside_func()
def outside_func():
print('outside called')
instance = MainWindow()
MainWindow.class_func2(MainWindow)
You should take a look to #staticmethod
class MainWindow():
def __init__(self):
print("in init")
def claas_func(self):
print ("func1")
#staticmethod
def class_func2():
return outside_func()
def outside_func():
print('outside called')
instance = MainWindow()
>> in init
instance.class_func2()
>> outside called
this #staticmethod (which itself it's something really cool called 'decorator') will make the method itself entirely callable without having pass 'self'.
Hope it helps
Try this instead. You have created an instance of MainWindow(). Now you can access its members with that.
class MainWindow():
def __init__(self):
print("in init")
def claas_func(self):
print ("func1")
def class_func2(self):
outside_func()
def outside_func():
print('outside called')
instance = MainWindow()
instance.class_func2()
Also run this and notice it initializes the MainWindow() class 2x. I DO NOT recommend this second approach. It is redundant and not proper. But just so you can kind of see what it is doing.
class MainWindow():
def __init__(self):
print("in init")
def claas_func(self):
print ("func1")
def class_func2(self):
outside_func()
def outside_func():
print('outside called')
instance = MainWindow()
MainWindow().class_func2()
I want to do something like this:
class worker():
def run(self):
# Do something
class main():
def __init__(self):
worker_obj = worker()
main_obj = main()
main_obj.worker_obj.run()
Is something like this wrong/possible?
You need to attach the attribute to self to make it available to use outside the function. Currently you are creating a function variable and not an instance attribute.
Change your class to below
class worker():
def run(self):
# Do something
class main():
def __init__(self):
self.worker_obj = worker()
main_obj = main()
main_obj.worker_obj.run()
You need to assign worker_obj as an attribute of the instance (self):
class main():
def __init__(self):
self.worker_obj = worker()
Otherwise, you are simply creating a local variable within __init__ that immediately goes out of scope.