I am trying to call a function in class A from One.py file in to Two.py file by using
from one import A
in this class A :in init function i am assigning a variable name i.e; self._fname=name.
Now i am trying to call a function funC() from class A which uses the self._fname varible like below in Two.py
now=A()
now.funC()
when call this function i am getting an Attribute Error :Class A has no attribute '_fname'
func():
if self._fname:
print "found"
How can i resolve this problem.Is there any way that i can import Class A object from one.py to Two.py so by using that object i can access the 'fname' variable.
AttributeError are typically raised when you try to access an attribute or member which is not defined in the object being dealt with. In your case,
you are initializing self._fname and are trying to use self.fname, which in my opinion is not defined in your class 'A' (notice the underscore before 'fname') . so basically, if you try to access the correct member (self._fname) in func() ,your Attribute error would go away !
Related
I have a python class that inherits from storm.py from the Apache Storm MultiLang project.
My class looks like the following:
import storm
class MyClassName(Storm.Bolt):
def initialize(self,conf,context):
self._conf = conf;
self._context = context
def process(self, in_tuple):
storm.ack(in_tuple)
if __name__ == '__main__':
MyClassName().run()
I copied my python file (myfilename.py) out to /usr/lib64/python2.7/site-package. I then logged into the python shell and did an import myfilename. That completed without error. When I run the following inspect.getmro(myfilename.MyClassName()) I get the following error:
AttributeError: 'MyClassName' object has no attribute '__bases__'
I was under the impression that when I declared my class and passed it Storm.Bolt that I was extending Storm.Bolt. My questions are:
Do I need to define __bases__ in my class?
What else am I missing?
Using Python 2.7.13 on CentOs7. Storm version is 1.1.0
The inspect.getmro function expects its argument to be a class, but you're passing it an instance. Get rid of the parentheses that call the class and your code should work:
inspect.getmro(myfilename.MyClassName) # not MyClassName()!
If the call you gave in the question was a simplified example and you don't have the class directly available where you're calling getmro on the instance, you can use type to get the class:
obj = SomeClass() # this happens somewhere earlier on, and we don't know SomeClass below
inspect.getmro(type(obj)) # but we can easily get it using type()
i made a test app for reasons like this. I am trying to have the ButtonRelease-1 event call a function inside another file. I am getting a syntax when trying to run the app.
TypeError: listb() takes exactly 2 arguments (1 given)
this is pretty strait forward syntax but i cannot fix it in this specific situation. I am basically just having the event get the clicked info printed. It's the event that is no working because function inside other file is not reconizing the event?
anyways, curious how to fix this code so it works. The function has to stay in another file. This would be easy if it was in same file but it cannot be.
start.py
from Tkinter import *
import example_funcs as EF
class Page_three(Frame):
def __init__(self):
Frame.__init__(self)
self.pack()
self.listboxs()
def listboxs(self):
self.z = Listbox(self)
self.z.grid()
for item in range(1,10):
self.z.insert(END, item)
self.z.bind("<ButtonRelease-1>", EF.listb(self))
root = Tk()
app = Page_three()
app.mainloop()
example_funcs.py
from Tkinter import *
import Tkinter as tk
def listb(self, event):
selection = self.z.curselection()
print selection
self is used so variables can be called inside the function, if do not call self as instance it will have syntax of not finding my listbox variable.
Passing EF.listb(self) doesn't do what you want it to do. It doesn't partially bind the self parameter to the instance you're calling it from, then let the event paramter get filled in by the callback. Instead, it just calls the function immediately (before the bind call is made) and you get an error about using the wrong number of arguments.
There are a few different ways you could fix this issue.
One option would be to manually bind the self parameter to the listb function using functools.partial:
import example_funcs as EF
import functools
class Page_three(Frame):
...
def listboxs(self):
...
self.z.bind("<ButtonRelease-1>", functools.partial(EF.listb, self)) # bind self
Another approach would be to make listb an actual method in your class, so that you can reference it as a method on self. That could look like this:
import example_funcs as EF
class Page_three(Frame):
...
def listboxs(self):
...
self.z.bind("<ButtonRelease-1>", self.listb) # refer to a method without calling it
listb = EF.listb # add the function from the other module as a method on this class
If listb isn't used anywhere else though, then defining it in another module and copying it over here would be pretty silly. You should just move the definition into this class instead of adding a reference to it after the fact. On the other hand, if listb is being used in several different classes, it suggests that the classes should be using some kind of inheritance to share the method, rather than crudely copying references to the one definition around.
This is a an example of the code:
module1.py is imported in the main.
In modul1.py, there is an init() function that creates classes from a previous imported library, and then, other functions uses this instance of the class, and the methods of that class.
ERROR: global name name1 not defined
module1.py:
from lib import class1, classs2
def init():
name1.class1()
def function():
name1.class1method1()
main.py:
import module1
init()
function()
I need some help, thanks
I think you may be getting confused between creating an object from a class definition and accessing the methods of the class. You are getting a not defined error because you have yet to define name1.
With the following adjustments, your code would work:
module1.py:
from lib import class1, classs2
def Init():
global name1
name1 = class1()
def function():
name1.class1method1()
main.py:
import module1
module1.Init()
module1.function()
That being said global variables are a bad idea, so the above code is for demonstration purposes only, not for actual use.
This is my first time to see the following codes.
dataset = type('dummy', (), {})()
And I print the dataset in the console it tells me that
<__main__.dummy at Ox7feec5195e90>
Can anyone help me to figure what these codes mean?
type here is metaclass in python. The meaning of metaclass is that they create classes for us.
And the code above means that we create a class named dummy(the first parameter). The class does not inherit from any other classes, so the second parameter is ().The class does not have any attributes and methods, so the third parameter is {}.
If we want to create a class named pp including attribute a and method m and let the class inherited from class f, we can code like this:
class f():
def __init__(self):
pass
def m():
print (123)
metaclass = type("pp", (f,), {"a":33, "m":m})()
I have a small question about static variable and TypeObjects.
I use the API C to wrap a c++ object (let's call it Acpp) that has a static variable called x.
Let's call my TypeObject A_Object :
typedef struct {
PyObject_HEAD
Acpp* a;
} A_Object;
The TypeObject is attached to my python module "myMod" as "A". I have defined getter and setters (tp_getset) so that I can access and modify the static variable of Acpp from python :
>>> import myMod
>>> myA1 = myMod.A(some args...)
>>> myA1.x = 34 # using the setter to set the static variable of Acpp
>>> myA2 = myMod.A(some other args...)
>>> print myA2.x
34
>>> # Ok it works !
This solution works but it's not really "clean". I would like to access the static variable in python by using the TypeObject and not the instances :
>>> import myMod
>>> myMod.A.x = 34 # what I wish...
Does anybody have an idea to help me ?
Thanks in advance.
Essentially, what you're trying to do is define a "static property". That is, you want a function to be called when you get/set an attribute of the class.
With that in mind, you might find this thread interesting. It only talks about Python-level solutions to this problem, not C extension types, but it covers the basic principles.
To implement the solution proposed in that thread for a C extension type, I think you'd have to initialize tp_dict and add to it an entry for "x" whose value is an object that implements __get__ appropriately.
You could add a dummy 'x' field in the A_Object and create a pair of set/get methods. When you access the dummy 'x' field, the method would redirect the call to the static 'x' field.