Class Attribute vs. Instance Attribute
I will show what is the difference between class and instance attribute. If you have some instances the class attribute will be affected by all of them, but the instance attribute just by the own instance.
1
2
3
4
5
6
7
8
9
10
11
12
|
class
Name
():
count = 0
def
__init__
(self, name):
self
.name = name
Name.count += 1
man1 = Name('David')
man2 = Name('Jozsi')
print Name.count
# Result: 2 #
print man2.name
# Result: 'Jozsi' #
|
Lambda function bug
Sometimes the lambda function does not want to work. You do everything on right way but you've got error.
1
2
3
4
|
# If it's not working:
lamda
x: call_function()
# Try this:
lamda
: call_function()
|
For statement an other way
Today I learnt a new way to use 'for' statement. It is really useful if we want to give a simple function to every item:
1
2
3
4
5
|
# We have a list
lst = ['a_1', 'b_2', 'c_3']
numbers = [str.split('_')
# equivalent with this:
numbers =
map
(
lambda
str: str.split('_')[1], lst)
|
Map function
There is the "map" function in pyton that is very handy. I show how it works.
1
2
3
4
5
6
7
8
9
10
|
# Example 1.
# We define a function first.
def
myFunction
(input): return input+1
result =
map
(myFunction, [1,2,3,4])
print result[1]
# Result: 3 #
# Example 2.
map
(
lambda
x : x+1, [1,2,3])
# Result: [-1, 1, 3] #
|
Class inheritance
I wrote a little template for class inheritance with super function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
class
A
(object):
def
__init__
(
self
, name=
'alma'
, *args, **kwds):
super
(A,
self
).__init__( *args, **kwds )
self
.name = name
def
getName
(
self
):
print self
.name
class
B
(A):
def
__init__
(
self
, number=12
, *args, **kwds):
super
(B,
self
).__init__( *args, **kwds )
self
.number = number
def
getNumber
(
self
):
print self
.number
b_object = B()
b_object.getName()
# Result: 'alma' #
|
SetAttr command
I wrote a script some days ago and I had a problem with cmds.setAttr's keyable flag. If you want to lock and hide a double3-type-attribute you need to set separately each attribute.
# Wrong:
cmds.setAttr('pCube1.t', k=False)
# Correct:
for axis in ['x', 'y', 'z']:
cmds.setAttr('pCube1.t'+axis, k=False)
# Wrong:
cmds.setAttr('pCube1.t', k=False)
# Correct:
for axis in ['x', 'y', 'z']:
cmds.setAttr('pCube1.t'+axis, k=False)
No comments:
Post a Comment