示例#1
0
文件: ex40.py 项目: mattman555/python
import mystuff  #Bring in the my stuff programs procdure

myStuff = {
    'apple': "apples"
}  #Create dictionary containing the word apple with the name apple


class Mystuff(object):  #Make a class with the name Mystuff
    def __init__(self):  #an initialization procedure with the object self
        self.tangerine = "And now a thousand years between"  #make an variable tangerine containing the string

    def apple(self):  #procedure with the object self
        print "I AM CLASSY APPLES!"


thing = Mystuff()  #give thing all the propreties of the Mystuff class
thing.apple()  #call the apple procedure from the Mystuff class through thing
print thing.tangerine  #print the contents of the tangerine variable
print myStuff[
    'apple']  #Print what is under the apple keyword in the myStuff dictionary
mystuff.apple()  #run the apple procedure imported from the mystuff module
print mystuff.tangerine  #print the tangerine variable from the mystuff module
my_stuff = {'apple':'I AM APPLES!'}

# prints I AM APPLES!
print my_stuff['apple'] # get apple from a dict


##### Modules are like dictionaries ####
### A module is a specialized dictionary that can store Python code
#  so you can access it using "." operator.

# A module is a Python file with some functions or variables in it.
# This file must be imported before you can access it in other Python file.
# You can access a module after importing it using "." operator.

mystuff.apple() # calls the apple() method in mystuff module.

print mystuff.tangerine # access variable tangerine in mystuff module.

#### Classes are like mini-modules ####
### A class is a way to take a grouping of functions and data and place
# them inside a container so you can access them with the '.' operator.

# Classes are like blueprints or definitions for creating new mini-modules.
# Instantiation is how you make one of these mini-modules and import it at the same time.
# The resulting created mini-module is called an object and you then assign it to
# a variable to work with it.

class MyStuff(object):
    def __init__(self):
        self.tangerine = "And now thousand years between"
示例#3
0
import mystuff

mystuff.apple()
print(mystuff.tangerine)

mystuff['яблоко']  # получаем apple из словаря
mystuff.apple()  # получаем apple из модуля
mystuff.tangerine  # аналогично, это обычная переменная
示例#4
0
#### Dictionaries ####

my_stuff = {'apple': 'I AM APPLES!'}

# prints I AM APPLES!
print my_stuff['apple']  # get apple from a dict

##### Modules are like dictionaries ####
### A module is a specialized dictionary that can store Python code
#  so you can access it using "." operator.

# A module is a Python file with some functions or variables in it.
# This file must be imported before you can access it in other Python file.
# You can access a module after importing it using "." operator.

mystuff.apple()  # calls the apple() method in mystuff module.

print mystuff.tangerine  # access variable tangerine in mystuff module.

#### Classes are like mini-modules ####
### A class is a way to take a grouping of functions and data and place
# them inside a container so you can access them with the '.' operator.

# Classes are like blueprints or definitions for creating new mini-modules.
# Instantiation is how you make one of these mini-modules and import it at the same time.
# The resulting created mini-module is called an object and you then assign it to
# a variable to work with it.


class MyStuff(object):
    def __init__(self):
示例#5
0
import mystuff

mystuff['apple'] # get apple from dict
mystuff.apple() # get apple from module
mystuff.tangerine # same thing, it's just a variable
示例#6
0
#dict style
mystuff = {'apple': "I am apples!"}
print(mystuff['apple'])


#module style
#this goes in mystuff.py
def apple():
    print("I am apples!")


tangerine = "Living reflection of a dream"

import mystuff

mystuff.apple()  #利用module访问函数
print(mystuff.tangerine)  #利用module访问变量

#class style


class Mystuff(object):
    def __init__(self):  ##将类实例化得到object
        self.tangerine = "And now a thousand years between"

    def apple(self):
        print("I am classy apples!")


thing = Mystuff()
thing.apple()
示例#7
0
import mystuff

print mystuff.apple()
print mystuff.tangerine
mystuff_object = mystuff.MyStuff()
mystuff_object.apple()

class Song(object):

    def __init__(self, lyrics):
        self.lyrics = lyrics

    def sing_me_a_song(self):
        for line in self.lyrics:
            print line

happy_bday = Song(["Happy birthdaty to you", "I don't want to get sued", "So I'll stop right there"])
bulls_on_parade = Song(["They rally around tha family", "With pockets full of shells"])

happy_bday.sing_me_a_song()
bulls_on_parade.sing_me_a_song()
示例#8
0
# function in it called apple, and a variable called tangerine.


# This goes in mystuff.py
def apple():
    print("I AM APPLES!")


tangerine = "Living reflection of a dream"

# I can then use the module mystuff with import and then access the apple
# function:
#

import mystuff
mystuff.apple()  # Have to reference where the apple function comes from
# I think before you were importing 'thing as thing' to make it shorter
print(mystuff.tangerine)

# Let's compare syntax:

mystuff['apple']  # get apple from dict
mystuff.apple()  # get function apple from the module
mystuff.tangerine  # same thing, it's just a variable

# This means we have a very common pattern in Python:
# 1. Take a key=value style container.
# 2. Get something out of it by the key’s name.

# In the case of the dictionary, the key is a string and the syntax is
# [key].
示例#9
0
#
#  Exercise 40: Modules, Classes, and Objects   
#
#   
#   
#   
#  
#
#  
#  __________________________________________________
#
# 
#
#---------------------------------------------------------|

import mystuff
mystuff.apple()  #executes 'apples' function
print mystuff.tangerine

#  Modules are like Dictionaries

mystuff = {'apple':  "I AM APPLES!"}
print mystuff['apple']  # prints the value for 'apples'

# Module review
#  1. A Python file with some functions
#  2. You import the file
#  3. You can then access functions or variables
#     using the . (dot) operator

#mystuff.apple()
示例#10
0
# this
mystuff = {"apple": "I AM APPLES!"}
print(mystuff["apple"])

# is the same as this, because there I defined the apple function as being print("I AM APPLES!")
import mystuff
mystuff.apple() # note difference between 7 and 8; 7 is calling a FUNCTION from MODULE mystuff; 8 is calling a variable defined there
print(mystuff.tangerine)

# very common pattern in Python: 1, take a key=value style container; 2, get something out of it by the key's name
# e.g., in a dict, the key is a string and the syntax is [key]; in a module, key is an identifier, and the syntax is .key (dot)
# class is similar: take functions and data and place inside a class, and access them with . (dot)

class MyStuff(object):

    def __init__(self):
        self.tangerine = "And now a thousand years between" # SELF refers to the class itself

    def apple(self):
        print("I AM CLASSY APPLES!")

# if a class is a mini-module, there has to be a concept similar to IMPORT for classes; it's called INSTANTIATE (i.e., create); when you INSTANTIATE, you get an OBJECT
## INSTANTIATE is to CLASS what IMPORT is to MODULE
## INSTANTIATE means "create an object from a class"; you INSTANTIATE a class by calling the class like you'd call a function (i.e., variable = function())
thing = MyStuff()
thing.apple() # remember the idea of "with open("doc.txt") as f", f would work just like THING here
print(thing.tangerine)

# Getting Things from Things
# dict style
print(mystuff["apple"]) # pylint shows "error" because ln 6 imports a module of the same name as the dict, and it gets confused, but it's fine
示例#11
0
文件: ex40a.py 项目: xytnba/LP3THW
#-------------
# 你可以通过import mystuff 这个module的方法引入
import mystuff
mystuff,apple()

#-------
#你也可以在里面添加一个变量named -----tangerine
def apple():
    print("I AM APPLES!")
tangerine = "Living reflection of dream"#这里仅仅是个变量而已。
#------
当然还有同样的方法:
import mystuff

mystuff.apple()
print(mystuff.tangerine)

# 回顾一下“字典”的语法,非常类似,但是语法也有所不同。下面进行比较:
mystuff['apple']#从字典里得到apple
mystuff.apple()#从 Module里得到apple的方法。
mystuff.tangerine # 同样的,这里只是个变量而已。

# 这意味着 我们有一个“非常”普通的pattern里。
#1.获得一个 key= 数值类型容器
#2.通过 key 的名字得出一些东西。
'''
总结:
A1   当它是字典的时候,这个 key 是一个字符串,其语法是 [key].
A2   当它是module的时候,这个key是一个 辨识符(identifier),语法是 .key
示例#12
0
mystuff = {'apple': "I AM APPLES!"}
print(mystuff['apples'])


#This goes in mystuff.py
def apple():
    print("I AM APPLES!")


import mystuff
mystuff.apple()

#This is an variable
tangerine = "Living reflection of a dream"

import mystuff
mystuff.apple()
print(mystuff.tangerine)

mystuff['apple']  #Get apple form dict
mystuff.apple()  #Get apple fromt the module
mystuff.tangerine  #Same thing, it's just a variable.

#Dict Style
mystuff['apples']

#Module Style
mystuff.apples()
print(mystuff.tangerine)

#Class Style
示例#13
0
mystuff = {'apple': "I AM APPLES"} # {} makes dictionary. It means 'apple' is 'I AM APPLES'
print(mystuff['apple']) # print 'apple' from 'mystuff' dictionary

import mystuff # take mystuff.py in this py file

mystuff.apple() # in mystuff.py, order apple is defined
print(mystuff.tangerine) # printing tangerine in mystuff.py


class MyStuff(object): # 'class' can making order. we make order 'MyStuff'
    def __init__(self):
        self.tangerine = "And now a thousand years between"
# __init__ is initialize. we define tangerine already. but using __init__, we make new tangerine

    def apple(self):
            print("I AM CLASSY APPLES!")


# end class MyStuff

thing = MyStuff()
thing.apple()
print(thing.tangerine)


class Song(object):
    def __init__(self, lyrics):
        self.lyrics = lyrics

    def sing_me_a_song(self):
        for line in self.lyrics:
示例#14
0
mystuff = {'apple': "I AM APPLES!"}
print(mystuff['apple'])  # key is 'apple'


# this is a module mystuff.py
def apple():
    print("I AM APPLES")


import mystuff
mystuff.apple()


def apple():
    print("I AM APPLES!")


# this is just a variable
tangerine = "Living reflection of a dream"

import mystuff
mystuff.apple()
print(mystuff.tangerine)

mystuff['apple']  # get apple from dict
mystuff.apple()  # get apple from module
mystuff.tangerine  # same thing, it's just a variable

# dict style
mystuff['apples']
#mystuff = {'apple': "I AM APPLES!"}
#print(mystuff['apple'])

import mystuff
#mystuff.apple()
#print(mystuff.tangerine)

mystuff['apple']  # get apple from dict
mystuff.apple()  # get apple from the module
mystuff.tangerine  # same thing, it's just a variable
示例#16
0
import mystuff

mystuff.apple()
print(mystuff.tangerine)

# mystuff['apple'] # get apple from dict
mystuff.apple() # get apple from the module
mystuff.tangerine # same thing, it's just a variable
示例#17
0
import mystuff as my
my.apple()
print(my.name)
示例#18
0
# -*-coding:utf-8


mystuff = {'apple' : "I AM APPLES!"}
print (mystuff['apple'])

import mystuff

mystuff.apple()
print(mystuff.tangerine)


class MyStuff(object):
    def __init__(self):
        self.tangerine = "And now a thousand years between"

    def apple(self):
        print("I AM CLASSY APPLES!")


# end class MyStuff

thing = MyStuff()
thing.apple()
print(thing.tangerine)


class Song(object):
    def __init__(self, lyrics):
        self.lyrics = lyrics
示例#19
0
# -*-coding:utf8
# http://learnpythonthehardway.org/book/ex40.html

mystuff = {'apple': "I AM APPLES!"}
print(mystuff['apple'])         # mystuff['apple'] 은 get apple from dict

import mystuff

mystuff.apple()                   # mystuff.apple() 은 get apple from the module
print(mystuff.tangerine)         # mystuff.tangerine 은 same thing, it's just a vatiable

class MyStuff(object):

    def __init__(self):
        self.tangerine = "And now a thounsand years between"

    def apple(self):
        print("I AN CLASSY APPLES!")
# end class MyStuff

thing = MyStuff()
thing.apple()
print(thing.tangerine)


class Song(object):

    def __init__(self, lyrics):
        self.lyrics = lyrics

    def sing_me_a_song(self):
示例#20
0
import mystuff

mystuff.apple()

print(mystuff.tangerine)
示例#21
0
文件: test.py 项目: nhost2676/python
import mystuff

mystuff.apple()  # truy cap vao ham
print mystuff.a  # truy cap vao bien