[Python] Using Self parameter in a Function in python to write method of NGram model to...

Discussão em 'Python' iniciado por Stack, Junho 27, 2025.

  1. Stack

    Stack Membro Participativo

    I have been applying the python programming to write a method to call NGram Model to perform a tokenizatin to segregate the webscrapped content from Financial website to multiple words called tokens either it could be a 1-Gram(1 word at at time), 2-Gram(2 words at a time) etc... I am keep on receving issues with usage of self keyword in the call of an object due to an attribute URL issue.

    Code:

    calling from outside of a class.

    X_Content = N_model_object.Web_scrapping(URL,Content);


    Method definition:

    def Web_scrapping(self,URL,Content):
    URL = self.URL;
    Content = self.Content;
    with urs.urlopen(URL):
    response = urs.response(URL);
    Content = response.read();
    return str(Content);


    Error:


    AttributeError Traceback (most recent call last) /tmp/ipython-input-6-822737383.py in <cell line: 0>() 87 #print('Authentication of X website for user f"{username},{password} had been completed successfully.',N_model_object.hashfunction(username)); 88 89 X_Content = N_model_object.Web_scrapping(URL,Content); 90 Tokens = Tokenization(X_Content); 91 i=0;


    /tmp/ipython-input-6-822737383.py in Web_scrapping(self, URL, Content) 27 28 def Web_scrapping(self,URL,Content): 29 URL = self.URL; 30 Content = self.Content; 31 with urs.urlopen(URL):


    AttributeError: 'N_Model' object has no attribute 'URL'
    ​

    There is an attribute URL present in the calling step and the method is using the argument called URL. There is an object called N_model_object is used to call Web_scrapping(URL,Content) from the class N_Model. Interestingly it use to execute the code earlier. Suggestion's would be appreciable.

    I have tried to perform the execution with Self and Without self keyword in the method definitions and am able to run the code without self for some time and have updated code in the other parts of the methods in the same class and while executing with the updated code am facing the above error.

    Continue reading...

Compartilhe esta Página