在嵌套函数中,我想调用内部函数

时间:2018-09-01 10:44:09

标签: python

我有一个叫做IrisData的类。我已经在其中定义了一个函数作为描述。

  • 说明具有多个我要访问的子功能。
  • 我希望我的功能像

    1. 如果调用了description,它将返回在description中定义的每个函数。 代码行:print(I.description())

    2. 在调用内部函数时,它应该仅返回内部函数。 代码行:print(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())

2 个答案:

答案 0 :(得分:0)

您需要使描述成为其自己的类的一部分。然后,您需要确定它是包含数据类的属性还是返回它的函数。检索它的语法是不同的。您需要的代码结构(遵循您现有的样式)的轮廓可能是:

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)

答案 1 :(得分:0)

您可能会想更改一些变量名,但这应该可以完成您要存档的操作:

<?php

include ('../data/config.php');    
include('../data/conn.php');

$subject = $_POST['naam_opleiding'];  
$from_address   = $_POST['email_medewerker'];
$to_name   = $_POST['docent'];
$to_address   = $_POST['email_docent'];
$to_address2   = $_POST['email_medewerker'];
$to_cc   = $_POST['email_secretaresse'];
$startTime = $_POST['startTime'];        
$endTime = $_POST['endTime'];  
$aanhef = $_POST['aanhef']; 
$tekst = $_POST['tekst']; 
$college = $_POST['college']; 
$naam_opleiding = $_POST['naam_opleiding']; 
$datum = $_POST['datum']; 
$begintijd = $_POST['begintijd']; 
$eindtijd = $_POST['eindtijd']; 
$bedrag = $_POST['bedrag']; 
$reiskosten = $_POST['reiskosten']; 
$voornaam_medewerker = $_POST['voornaam_medewerker']; 
$medewerker = $_POST['medewerker']; 
$collegezaal = $_POST['collegezaal']; 
$doccol = $_POST['doccol']; 
$oplcol = $_POST['oplcol']; 

$domain = 'asreconnect.nl';

//Create Email Headers
$mime_boundary = "----Meeting Booking----".MD5(TIME());

$headers = "From: ".$medewerker." <".$from_address.">\n";
$headers .= "CC: ".$to_cc."\r\n";
$headers .= "Reply-To: ".$medewerker." <".$from_address.">\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/alternative; boundary=\"$mime_boundary\"\n";
$headers .= "Content-class: urn:content-classes:calendarmessage\n";

//Create Email Body (HTML)
$message = "--$mime_boundary\r\n";
$message .= "Content-Type: text/html; charset=UTF-8\n";
$message .= "Content-Transfer-Encoding: 8bit\n\n";
$message .= "<html>\n";
$message .= "<body>\n";
$message .= '<p>Opleiding: <b>'.$naam_opleiding.'</b></p>';
$message .= '<p>College: <b>'.$college.'</b></p>';
$message .= '<p>Datum: <b>'.$datum.'</b> van <b>'.$begintijd.'</b> t/m <b>'.$eindtijd.'</b></p>';
$message .= '<p> '.$aanhef.' </p>';
$message .= '<p>'.$tekst.' </p>';
$message .= '<p>Alvast bedankt!</p>';
$message .= '<p>Hartelijke groet,</p>';
$message .= '<p>'.$voornaam_medewerker.'</p>';
$message .= "</body>\n";
$message .= "</html>\n";
$message .= "--$mime_boundary\r\n";

$ical = 'BEGIN:VCALENDAR' . "\r\n" .
'PRODID:-//Microsoft Corporation//Outlook 10.0 MIMEDIR//EN' . "\r\n" .
'VERSION:2.0' . "\r\n" .
'METHOD:REQUEST' . "\r\n" .
'BEGIN:VTIMEZONE' . "\r\n" .
'TZID:CEST' . "\r\n" .
'BEGIN:STANDARD' . "\r\n" .
'DTSTART:20091101T020000' . "\r\n" .
'RRULE:FREQ=YEARLY;INTERVAL=1;BYDAY=1SU;BYMONTH=11' . "\r\n" .
'TZOFFSETFROM:+0000' . "\r\n" .
'TZOFFSETTO:+0200' . "\r\n" .
'TZNAME:EST' . "\r\n" .
'END:STANDARD' . "\r\n" .
'BEGIN:DAYLIGHT' . "\r\n" .
'DTSTART:20090301T020000' . "\r\n" .
'RRULE:FREQ=YEARLY;INTERVAL=1;BYDAY=2SU;BYMONTH=3' . "\r\n" .
'TZOFFSETFROM:-0000' . "\r\n" .
'TZOFFSETTO:+0200' . "\r\n" .
'TZNAME:EDST' . "\r\n" .
'END:DAYLIGHT' . "\r\n" .
'END:VTIMEZONE' . "\r\n" .  
'BEGIN:VEVENT' . "\r\n" .
'ORGANIZER;CN="'.$medewerker.'":MAILTO:'.$from_address. "\r\n" .
'ATTENDEE;CN="'.$to_name.'";ROLE=REQ-PARTICIPANT;RSVP=TRUE:MAILTO:'.$to_address. "\r\n" .
'LAST-MODIFIED:' . date("Ymd\TGis") . "\r\n" .
'UID:'.date("Ymd\TGis", strtotime($startTime)).rand()."@".$domain."\r\n" .
'DTSTAMP:'.date("Ymd\TGis"). "\r\n" .
'DTSTART;TZID="CEST":'.date("Ymd\THis", strtotime($startTime)). "\r\n" .
'DTEND;TZID="CEST":'.date("Ymd\THis", strtotime($endTime)). "\r\n" .
'TRANSP:OPAQUE'. "\r\n" .
'SEQUENCE:1'. "\r\n" .
'SUMMARY:' . $oplcol . "\r\n" .
'LOCATION:' . $collegezaal . "\r\n" .
'CLASS:PUBLIC'. "\r\n" .
'PRIORITY:5'. "\r\n" .
'BEGIN:VALARM' . "\r\n" .
'TRIGGER:-PT15M' . "\r\n" .
'ACTION:DISPLAY' . "\r\n" .
'DESCRIPTION:Reminder' . "\r\n" .
'END:VALARM' . "\r\n" .
'END:VEVENT'. "\r\n" .
'END:VCALENDAR'. "\r\n";
$message .= 'Content-Type: text/calendar;name="meeting.ics";method=REQUEST'."\n";
$message .= "Content-Transfer-Encoding: 8bit\n\n";
$message .= $ical;

$mailsent = mail($to_address, $subject, $message, $headers);

return ($mailsent)?(true):(false);

?>

我将description方法更改为一个类,因此它可以存储变量。这使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()) 成为可能,您无法在python中调用函数。另外,新类是在IrisData类的构造函数中创建的,因此在创建IrisData对象时都会创建它们。