Package.Name来自哪里?

huangapple go评论142阅读模式
英文:

Where does Package.Name come from?

问题

[package.name]只是键的变量名,package.name是从package对象中获取的。

英文:

In the add_packages method below, is [package.name] just a variable name for the key? Where do we get package.name from?

 class Repository:
   def __init__(self):
     self.packages = {}
    def add_package(self, package):
     self.packages[package.name] = package

答案1

得分: 1

package.name 是一个 表达式;表达式的值是 package 所引用的对象的 name 属性的值。packageadd_package 方法的一个参数名称。当你 调用 这个方法时,你提供的值作为参数与名称 package 绑定。

例如,

class Package:
    def __init__(self, name):
        self.name = name

p = Package("foo")  # p.name == "foo"

r = Repository()
r.add_package(p)  # self.packages["foo"] = p
英文:

package.name is an expression; the value of the expression is the value of the name attribute of whatever object package refers to. package is the name of a parameter of the add_package method. When you call this method, the value you provide as an argument is bound to the name package.

For example,

class Package:
    def __init__(self, name):
        self.name = name


p = Package("foo")  # p.name == "foo"

r = Respository()
r.add_package(p)  # self.packages["foo"] = p

huangapple
  • 本文由 发表于 2023年3月4日 03:16:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75631055.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定