如何访问python中成员函数中指定的该类成员属性?

发布于 2025-02-07 12:23:38 字数 814 浏览 1 评论 0原文

我正在使用Selenium从事一个Python项目,我需要访问另一个成员函数中指定值的成员属性,但我似乎无法弄清楚,尽管我知道这很容易解决,这是我的代码和我的内容到目前为止尝试了

`class User_data:
'''Get important info ab user'''
def __init__ (self, url, product_name):
    self.url = url
    self.product_name = product_name

def askFor_info (self):
    self.url = str(input("Please enter the url of the site you wish to make purchase from: "))
    self.prodcut_name = str(input("Please enter the name of the product you wish to purchase: "))

    # person = User_data(url = self.url, product_name= self.product_name)

    print(f"{self.url},", f"{self.prodcut_name}")

这是错误的代码

driver.get(user_data.askfor_info.url)

当我这样做时,我会发现一个错误,上面写着“ attributeError:'function' 对象没有属性“ url””

I'm working on a python project using selenium and I need to access a member attribute who's value is specified in another member function, I can't seem to figure it out although i know it's an easy fix here's my code and what I've tried so far

`class User_data:
'''Get important info ab user'''
def __init__ (self, url, product_name):
    self.url = url
    self.product_name = product_name

def askFor_info (self):
    self.url = str(input("Please enter the url of the site you wish to make purchase from: "))
    self.prodcut_name = str(input("Please enter the name of the product you wish to purchase: "))

    # person = User_data(url = self.url, product_name= self.product_name)

    print(f"{self.url},", f"{self.prodcut_name}")

this is the piece of code that's wrong

driver.get(User_data.askFor_info.url)

when i do this i get an error that says " AttributeError: 'function'
object has no attribute 'url'"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

帅的被狗咬 2025-02-14 12:23:38

askfor_info是一种方法,self.url是类成员,因此要访问该变量,首先您需要使用类实例化使用类

user_data = User_data("MY URL", "MY PRODUCT NAME")
# Then run the method askFor_info
user_data.askFor_info()
# Then you can fetch the url inputted by user by
user_data.url # Updated URL from USER INPUT
user_data.product_name # Updated Product Name from User Input

askFor_info is a method and self.url is a class member so to access that variable, First you need to instantiate the class using

user_data = User_data("MY URL", "MY PRODUCT NAME")
# Then run the method askFor_info
user_data.askFor_info()
# Then you can fetch the url inputted by user by
user_data.url # Updated URL from USER INPUT
user_data.product_name # Updated Product Name from User Input
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文