简体   繁体   中英

In nested function, I want to call inner function

I have a class called IrisData. I have defined One function in that as description.

  • description has multiple sub-function inside which I want to access.
  • I want my function to be like

    1. It should return every function defined within description, if description is called. code line : print(I.description())

    2. It should return only inner function when inner function is called. code line : print(I.description.attribute())*

PFB code snippet:

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())

You need to make the description part its own class. You then need to decide whether it's a property of the containing data class, or a function that returns it; the syntax to retrieve it is different. An outline of the code structure you need (following your existing style) could be:

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())

For simple data records like this, though, it's more common to just store things directly as properties of the object. I'd instead write:

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)

You may wonna change some of the variable names, but this should do what you want to archive:

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 changed the description method to a class, so it can store variables. That makes I.description.attribute() possible, you cannot call on functions in python. Also the new class is created in the constructor of the IrisData class, so they both get created when you create the IrisData object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM