python导入类属性不能用,Python将类属性导入方法本地命名空间

I have been wondering for a while if there is easier way to assign class attributes to method local namespace. For example, in dosomething method, I explicitly make references to self.a and self.b:

class test:

def __init__(self):

self.a = 10

self.b = 20

def dosomething(self):

a = self.a

b = self.b

return(a + b)

But sometimes I have a lot of variables (more than 10) and it gets messy to type and look at - I would have bunch of var = self.var statements at the beginning of a method.

Is there any way to do this more compact way? (I know updating local() is not a good idea)

Edit: Ideally, what I want is:

def dosomething(self):

populate_local_namespace('a', 'b')

return(a + b)

解决方案Q. Is there any way to do this more compact way?

1. If the variables are read-only, it would be reasonably Pythonic to factor-out a multi-variable accessor method:

class Test:

def __init__(self):

self.a = 10

self.b = 20

self.c = 30

def _read_vars(self):

return self.a, self.b, self.c

def dosomething(self):

a, b, c = self._read_vars()

return a + b * c

def dosomethingelse(self):

a, b, c = self._read_vars()

return a - b * c

If the variables aren't read-only, it is best to stick with self.inst_var = value. That is the normal way to write Python code and is usually what most people expect.

2. Once in a while you will see people abbreviate self with a shorter variable name. It is used when the readability benefits of decluttering outweigh the readability cost of using a non-standard variable name:

def updatesomethings(s):

s.a, s.b, s.c = s.a + s.c, s.b - s.a, s.c * s.b

3. Another way to handle a very large number instance variable is to store them in a mutable container for ease of packing and unpacking:

class Test:

def __init__(self, a, b, c, d, e, f, g, h, i):

self._vars = [a, b, c, d, e, f, g, h, i]

def fancy_stuff(self):

a, b, c, d, e, f, g, h, i = self._vars

a += d * h - g

b -= e * f - c

g = a + b - i

self._vars[:] = a, b, c, d, e, f, g, h, i

4. There is also a dictionary manipulation approach that would work, but it has a code smell that most Pythonistas would avoid:

def updatesomethings(self):

a = 100

b = 200

c = 300

vars(self).update(locals())

del self.self

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值