我有一个类似于以下课程的课程:
class Someclass(object):
def __init__(self, n1=5, n2=12):
self.n1 = n1
self.n2 = n2
我想在以下定义的函数中使用上述类中的__init__
的参数:
def Search(model: Someclass):
n11 = 10
n22 = 20
print( type(model.__init__), type(model) )
# I want to multiply self.n1 with n11 , and self.n2 with n22 using this function.
Search(Someclass)
>> <class 'function'> <class 'type'>
如何在__init__
内的Someclass
的{{1}}构造函数中访问元素?
答案 0 :(得分:3)
它们是类实例上的属性。如果是df['DOB']
0 01-01-84
1 31-07-85
2 24-08-85
3 30-12-93
4 09-12-77
5 08-09-90
6 01-06-88
7 04-10-89
8 15-11-91
9 01-06-68
Name: DOB, dtype: object
,则只需使用print(pd.to_datetime(df1['Date.of.Birth']))
0 1984-01-01
1 1985-07-31
2 1985-08-24
3 1993-12-30
4 1977-09-12
5 1990-08-09
6 1988-01-06
7 1989-04-10
8 1991-11-15
9 2068-01-06
Name: DOB, dtype: datetime64[ns]
和isinstance(m, Someclass)
:
m.n1
输出:
m.n2
这在 this 情况下有效,因为参数作为实例变量存储在实例上/实例上-在无法存储的参数上不起作用:
class Someclass(object):
def __init__(self, n1=5, n2=12):
self.n1 = n1
self.n2 = n2
def Search(model: Someclass):
n11 = 10
n22 = 20
# Like So:
mul = model.n1 * model.n2
print( type(model.__init__), type(model) , mul)
Search(Someclass(5,10))
Doku: