[英]In nested function, I want to call inner function
我有一個名為 IrisData 的類。 我在其中定義了一個函數作為描述。
我希望我的功能像
如果調用描述,它應該返回描述中定義的每個函數。 代碼行:打印(I.description())
當調用內部函數時,它應該只返回內部函數。 代碼行:打印(I.description.attribute())*
PFB 代碼片段:
class IrisData:
def urls(self):
self.url='https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'
return self.url
def columns(self):
self.column_name=['sepal length','sepal width','petal length','petal width','class']
return self.column_name
def description(self):
def title():
self.titles ='Title: Iris Plants Database'
return self.titles
def source():
self.sources='''Sources:
\t(a) Creator: R.A. Fisher
\t(b) Donor: Michael Marshall (MARSHALL%PLU@io.arc.nasa.gov)
\t(c) Date: July, 1988'''
return self.sources
def info():
self.descri='''Relevant Information:
\t--- This is perhaps the best known database to be found in the pattern recognition literature. Fisher's paper is a classic in the field and is referenced frequently to this day. (See Duda & Hart, for
example.
\t--- The data set contains 3 classes of 50 instances each, where each class refers to a type of iris plant. One class is linearly separable from the other 2; the latter are NOT linearly
separable from each other.
\t--- Predicted attribute: class of iris plant.
\t--- This is an exceedingly simple domain.
\t--- This data differs from the data presented in Fishers article (identified by Steve Chadwick, spchadwick@espeedaz.net )
\tThe 35th sample should be: 4.9,3.1,1.5,0.2,"Iris-setosa"
\twhere the error is in the fourth feature.
\tThe 38th sample: 4.9,3.6,1.4,0.1,"Iris-setosa"
\twhere the errors are in the second and third features. '''
return self.descri
def attribute():
self.attri="""Attribute Information:
1. sepal length in cm
2. sepal width in cm
3. petal length in cm
4. petal width in cm
5. class:
-- Iris Setosa
-- Iris Versicolour
-- Iris Virginica"""
return self.attri
return attribute(),info(),source(),title()
I=IrisData()
print(I.urls())
print(I.columns())
print(I.description())
print(I.description.attribute())
您需要將描述部分設為自己的類。 然后,您需要決定它是包含數據類的屬性,還是返回它的函數; 檢索它的語法是不同的。 您需要的代碼結構大綱(遵循您現有的風格)可以是:
class Description:
def title(self): ...
def source(self): ...
def info(self): ...
def attribute(self): ...
class IrisData:
def description(self):
self.desc = Description()
return self.desc
data = IrisData()
print(data.description())
print(data.description().attribute())
但是,對於像這樣的簡單數據記錄,更常見的是將事物直接存儲為對象的屬性。 我會寫:
class IrisData:
def __init__(self, url, description, ...):
self.url = url
self.description = description
# and set the other properties here
# but there are no accessor functions here
# and no functions that retrieve object state, but only by mutating it
data = IrisData('https://archive.ics.uci.edu/ml/...',
Description(...),
...)
print(data.url)
print(data.description.attribute)
您可能不會更改某些變量名稱,但這應該可以執行您想要存檔的操作:
class IrisData(object):
def __init__(self):
self.descript = self.des()
def urls(self):
self.url='https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'
return self.url
def columns(self):
self.column_name=['sepal length','sepal width','petal length','petal width','class']
return self.column_name
def description(self):
return self.descript.attribute(),self.descript.info(),self.descript.source(),self.descript.title()
class des():
def title(self):
self.titles ='Title: Iris Plants Database'
return self.titles
def source(self):
self.sources='''Sources:
\t(a) Creator: R.A. Fisher
\t(b) Donor: Michael Marshall (MARSHALL%PLU@io.arc.nasa.gov)
\t(c) Date: July, 1988'''
return self.sources
def info(self):
self.descri='''Relevant Information:
\t--- This is perhaps the best known database to be found in the pattern recognition literature. Fisher's paper is a classic in the field and is referenced frequently to this day. (See Duda & Hart, for
example.
\t--- The data set contains 3 classes of 50 instances each, where each class refers to a type of iris plant. One class is linearly separable from the other 2; the latter are NOT linearly
separable from each other.
\t--- Predicted attribute: class of iris plant.
\t--- This is an exceedingly simple domain.
\t--- This data differs from the data presented in Fishers article (identified by Steve Chadwick, spchadwick@espeedaz.net )
\tThe 35th sample should be: 4.9,3.1,1.5,0.2,"Iris-setosa"
\twhere the error is in the fourth feature.
\tThe 38th sample: 4.9,3.6,1.4,0.1,"Iris-setosa"
\twhere the errors are in the second and third features. '''
return self.descri
def attribute(self):
self.attri="""Attribute Information:
1. sepal length in cm
2. sepal width in cm
3. petal length in cm
4. petal width in cm
5. class:
-- Iris Setosa
-- Iris Versicolour
-- Iris Virginica"""
return self.attri
if __name__ == "__main__":
I=IrisData()
print(I.urls())
print(I.columns())
print(I.description())
print(I.descript.attribute())
我將描述方法更改為一個類,因此它可以存儲變量。 這使得I.description.attribute()
可能,你不能在 python 中調用函數。 此外,新類是在 IrisData 類的構造函數中創建的,因此當您創建 IrisData 對象時,它們都會被創建。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.