如果__init__中没有Pythonic方式设置变量

时间:2018-12-13 20:52:32

标签: python

实现这一目标的最pythonic或最优雅的方法是什么?

def __init__(self, connection=None, some=None, thing=None, else=None):
   if connection is None:
      self.connection = SetConnection()
   else:
      self.connection = connection
.
.

如果我有如上所述的多个输入参数,我想调用另一个类进行实例化。有什么好方法可以保持外观整洁而不冗长?

5 个答案:

答案 0 :(得分:4)

您可以使用二进制运算符:

def __init__(self, connection=None, some=None, thing=None, else=None):
    self.connection = connection or SetConnection()

如果连接为无(假),它将运行SetConnection()

答案 1 :(得分:1)

使用三元运算符

click

答案 2 :(得分:1)

如果出于其他原因,您不需要记录<?php $date = time() ; $day = date('d', $date) ; $month = date('m', $date); $year = date('Y', $date); $first_day = mktime(0,0,0,$month, 1, $year) ; $title = date('F', $first_day) ; $day_of_week = date('D', $first_day) ; switch($day_of_week){ case "Sun": $blank = 0; break; case "Mon": $blank = 1; break; case "Tue": $blank = 2; break; case "Wed": $blank = 3; break; case "Thu": $blank = 4; break; case "Fri": $blank = 5; break; case "Sat": $blank = 6; break; } if($month-1 != 0) { $last_month = $month-1; } else { $last_month = 12; } if($last_month == '12') { $year = $year-1; } $last_month_first_day = mktime(0,0,0,$last_month, 1, $year); $last_month_days_in_month = cal_days_in_month(0, $last_month, $year); $last_month_day_of_week = date('D', $last_mont_days_in_month); $last_month_days_to_add_to_last_month_end = $blank; $last_month_end = $last_month_days_in_month- $last_month_days_to_add_to_last_month_end; $days_in_month = cal_days_in_month(0, $month, $year) ; echo "<table border=1 width=294>"; echo "<tr><th colspan=7> $title $year </th></tr>"; switch($blank){ case "0": echo "<tr><td width=42>S</td><td width=42>M</td><td width=42>T</td><td width=42>W</td><td width=42>T</td><td width=42>F</td><td width=42>S</td></tr>"; break; case "1": echo "<tr><td width=42>M</td><td width=42>T</td><td width=42>W</td><td width=42>T</td><td width=42>F</td><td width=42>S</td><td width=42>S</td></tr>"; break; case "2": echo "<tr><td width=42>T</td><td width=42>W</td><td width=42>T</td><td width=42>F</td><td width=42>S</td><td width=42>S</td><td width=42>M</td></tr>"; break; case "3": echo "<tr><td width=42>W</td><td width=42>T</td><td width=42>F</td><td width=42>S</td><td width=42>S</td><td width=42>M</td><td width=42>T</td></tr>"; break; case "4": echo "<tr><td width=42>T</td><td width=42>F</td><td width=42>S</td><td width=42>S</td><td width=42>M</td><td width=42>T</td><td width=42>W</td></tr>"; break; case "5": echo "<tr><td width=42>F</td><td width=42>S</td><td width=42>S</td><td width=42>M</td><td width=42>T</td><td width=42>W</td><td width=42>T</td></tr>"; break; case "6": echo "<tr><td width=42>S</td><td width=42>S</td><td width=42>M</td><td width=42>T</td><td width=42>W</td><td width=42>T</td><td width=42>F</td></tr>"; break; default: echo "<tr><td width=42>S</td><td width=42>M</td><td width=42>T</td><td width=42>W</td><td width=42>T</td><td width=42>F</td></tr>"; } //This counts the days in the week, up to 7 $day_count = 1; echo "<tr>"; $day_num = 1; while ( $day_num <= $days_in_month ) { echo "<td><a href='/test/day.php?day=" . $day_num . "&month=" . $title . "&year=" . $year . "'>" . $day_num . '<br>'; echo "</a></td>"; $day_num++; $day_count++; if ($day_count > 7) { echo "</tr><tr>"; $day_count = 1; } } $end_days = 1; while ( $day_count >1 && $day_count <=7 ) { echo "<td><span style='color:grey;'> - </span></td>"; $day_count++; $end_days++; } echo "</tr></table>"; ?> 最初是作为connection传递的事实,那么您可以覆盖该名称:< / p>

None

答案 3 :(得分:0)

我的2c使用isinstance

def __init__(self, connection=None, some=None, thing=None, else=None):
   if isinstance(connection, "class/type of connection") :
       self.connection = connection    
   else:
       self.connection = SetConnection()

旧答案:

self.connection = connection if connection else SetConnection()

PS:我不知道class的{​​{1}}或type,如果您也不知道,请使用connection

答案 4 :(得分:0)

只是为了好玩,为什么不呢

def __init__(self, **kwargs):
    self.connection = kwargs.get('connection', SetConnection())

在使用pythonic时,这会忽略与虚假值相关的问题。话虽如此,它肯定还有其他缺点,例如那些与代码可自动记录性的想法有关。